标签:style blog color io os for sp div log
并查集
#include<iostream> #include<cstdio> using namespace std; #define MAXN 100005 int p[MAXN],N; int Find(int x) {return x == p[x] ? x : p[x] = Find(p[x]);} int main() { //freopen("sample.txt","r",stdin); int u, v; while (scanf("%d",&u) != EOF) { int ans = 0; if (u == -1) break; for (int i = 0; i < MAXN; i++) p[i] = i; scanf("%d",&v); int x = Find(u), y = Find(v); if (x != y) p[x] = y; else ans++; while (scanf("%d",&u) != EOF) { if (u == -1) break; scanf("%d",&v); int x = Find(u), y = Find(v); if (x != y) p[x] = y; else ans++; } printf("%d\n",ans); } return 0; }
标签:style blog color io os for sp div log
原文地址:http://www.cnblogs.com/Commence/p/4029875.html