标签:des style blog io color sp for 数据 div
2 3 1 1 2 3 2 3 2 1 2
2 1
#include<stdio.h> #define MAXN 10000 int bin[MAXN]; void init(int n) { int i; for(i=1; i<=n; i++) bin[i] = i; } int find(int x) { int t = x; while(t!=bin[t]) t = bin[t]; return t; } void merge(int x, int y) { int fx, fy; fx = find(x); fy = find(y); if(fx!=fy) bin[fx] = fy; } int main() { int t, n, m, i, cnt=0; scanf("%d", &t); while(t--) { int x, y; cnt = 0; scanf("%d%d", &n, &m); init(n); for(i=1; i<=m; i++) { scanf("%d %d", &x, &y); merge(x, y); } for(i=1; i<=n; i++) { if(bin[i]==i) cnt++; } printf("%d\n", cnt); } return 0; }
标签:des style blog io color sp for 数据 div
原文地址:http://www.cnblogs.com/6bing/p/4121899.html