标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18006 Accepted Submission(s): 8856
#include <stdio.h> int father[1010]; int find(int a) { int k, j, r; r = a ; while(r != father[r]) r = father[r]; k = a; while(k != r) { j = father[k]; father[k] = r; k = j; } return r; } /*int find(int a) { while(father[a] != a) father[a] = find(father[a]); return father[a]; } */ void mercy(int a, int b) { int p = find(a); int q = find(b); if(p != q) father[p] = q; } int main() { int i, t; scanf("%d", &t); while(t--) { int n, m, a, b; scanf("%d %d", &n, &m); for(i=1; i<=n; i++) father[i] = i; for(i=0; i<m; i++) { scanf("%d %d", &a, &b); mercy(a, b); } //printf("\n"); int total = 0; for(i=1; i<=n; i++) { if(father[i] == i) total++; } printf("%d\n", total); } return 0; }
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4679585.html