链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1213
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#problem/C
代码:
#include<stdio.h> #include<queue> #include<stack> #include<string.h> using namespace std; #define maxn 1005 #define oo 0xfffffff int n, m; int f[maxn]; int Find(int x) { if(x!=f[x]) f[x]=Find(f[x]); return f[x]; } int main() { int t, i; scanf("%d", &t); while(t--) { int a, b, fa, fb; scanf("%d%d", &n, &m); for(i=0; i<=n; i++) f[i]=i; for(i=1; i<=m; i++) { scanf("%d%d", &a, &b); fa=Find(a), fb=Find(b); if(fa!=fb) f[fa]=fb; } int sum=0; for(i=1; i<=n; i++) { if(f[i]==i) sum++; } printf("%d\n", sum); } return 0; }
勿忘初心