标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20996 Accepted Submission(s): 10391
1 #include <cstdio> 2 #include <algorithm> 3 #include <queue> 4 using namespace std; 5 const int maxn = 100050; 6 7 int f[maxn]; 8 9 int sf(int x){ return x == f[x] ? x : f[x] = sf(f[x]); } 10 int main() 11 { 12 int t, n, m, a, b, ans; 13 scanf("%d", &t); 14 while (t--){ 15 ans = 0; 16 scanf("%d%d", &n,&m); 17 for (int i = 1; i <= n; i++) f[i] = i; 18 for (int i = 1; i <= m; i++) 19 { 20 scanf("%d%d", &a, &b); 21 f[sf(a)] = sf(b); 22 } 23 for (int i = 1; i <= n; i++) 24 if (f[i] == i) ans++; 25 printf("%d\n", ans); 26 } 27 }
标签:
原文地址:http://www.cnblogs.com/cumulonimbus/p/5185066.html