#include<bits/stdc++.h> using namespace std; const int N = 1e5 + 10, M = 1e5 + 10, INF = 0x3f3f3f3f; int n, m, h[N], e[M << 1], ne[M << 1], idx, color[N]; void addEdge(int f, int t) { e[idx] = t, ne[idx] = h[f], h[f] = idx ++ ; } bool dfs(int u, int c) { color[u] = c; for(int i = h[u]; ~i; i = ne[i]) { int t = e[i]; if(!color[t] && !dfs(t, 3 - c)) return false; else if(color[t] == c) return false; } return true; } int main() { cin >> n >> m; memset(h, -1, sizeof h); for(int i = 0; i < m; i ++ ) { int f, t; cin >> f >> t; addEdge(f, t); addEdge(t, f); } bool flag = true; for(int i = 1; i <= n; i ++ ) { if(!color[i]) { flag = dfs(i, 1); if(!flag) break; } } if(flag) puts("Yes"); else puts("No"); return 0; }
标签:二分,10,int,1e5,染色法,addEdge From: https://www.cnblogs.com/leyuo/p/16646677.html