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 62 63 64 65
| #include<iostream> #include<cstdio> #include<cstring>
using namespace std;
const int N = 100010, M = 200010;
int h[N], e[M], ne[M], idx; int color[N];
int n, m;
void add(int x, int y) { e[idx] = y; ne[idx] = h[x]; h[x] = idx++; }
bool dfs(int u, int c) { color[u] = c; for(int i = h[u]; i != -1; i = ne[i]) { int j = e[i]; if(!color[j]) { if(!dfs(j, 3 - c)) return false; } else if(color[j] == c) return false; } return true; }
int main() { memset(h, -1, sizeof(h));
scanf("%d%d", &n, &m);
while (m--) { int x, y; scanf("%d%d", &x, &y); add(x, y), add(y, x); }
bool success = true; for(int i = 0; i < n; i++) { if(!color[i]) if(!dfs(i, 1)) { success = false; break; } }
if(success) puts("Yes"); else puts("No");
return 0; }
|