-
Notifications
You must be signed in to change notification settings - Fork 0
/
AcWing860.cpp
61 lines (61 loc) · 1.14 KB
/
AcWing860.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string.h>
using namespace std;
const int N = 100100;
//因为是无向图,采用我们邻接表的方式存储需要存储两次相同的边,所以M的大小要是2倍以上
const int M = 200100;
int e[M], ne[M], h[N], idx, n, m;
//存储染色的数组
int d[N];
void add(int a, int b)
{
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
bool dfs(int v, int color)
{
d[v] = color;
for (int i = h[v]; i != -1; i = ne[i])
{
int j = e[i];
if (d[j] == -1)
{
if (!dfs(j, !color))
{
return false;
}
}
if (d[j] == color)
{
return false;
}
}
return true;
}
int main()
{
cin >> n >> m;
memset(h, -1, sizeof(h));
int u, v;
for (int i = 0; i < m; i++)
{
cin >> u >> v;
add(u, v);
add(v, u);
}
memset(d, -1, sizeof(d));
for (int i = 1; i <= n; i++)
{
if (d[i] == -1)
{
if(!dfs(i, 0))
{
cout<<"No";
return 0;
}
}
}
cout<<"Yes";
return 0;
}