HihoCoder - 1121 二分图判定
#include #include #include #include using namespace std;const int maxn = 1e5 + 10;const int maxm = 4 * 1e5 + 10;int tot;int head[maxn];int vis[maxn];struct Edge { int to, next;}edge[maxm<<1];void init(int n) { tot = 0; for (int i = 0; i <= n; i++) head[i] = -1, vis[i] = 0;}void addedge(int u, int v) { tot++; edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot;}bool dfs(int u) { for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if (vis[v]) { if (vis[v] == -vis[u]) continue; else return false; } else { vis[v] = -vis[u]; if (!dfs(v)) return false; } } return true;}int main() { int T; scanf("%d", &T); for (int kase = 1; kase <= T; kase++) { int n, m; scanf("%d %d", &n, &m); init(n); for (int i = 1; i <= m; i++) { int u, v; scanf("%d %d", &u, &v); addedge(u, v); addedge(v, u); } bool ok = true; for (int i = 1; i <= n; i++) { if (!vis[i]) { vis[i] = 1; if (!dfs(i)) { ok = false; break; } } } if (ok) printf("Correct\n"); else printf("Wrong\n"); }}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~