标签:des style blog http color os io for
2 5 3 1 2 2 3 4 5 5 1 2 5
2 4
思路:裸的并查集
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<stdlib.h> 5 #include<math.h> 6 #include<algorithm> 7 #define LL long long 8 #define mod 1e9 + 7 9 const int M = 1005; 10 11 using namespace std; 12 13 struct node{ 14 int x; 15 int y; 16 }a[M]; 17 18 int cun[M]; 19 20 int find(int x) 21 { 22 return cun[x] == x ? x : cun[x] = find(cun[x]); 23 } 24 25 void shu(int x, int y) 26 { 27 int p = find(x); 28 int q = find(y); 29 cun[p] = q; 30 } 31 32 int main() 33 { 34 int n, m; 35 int t; 36 cin >> t; 37 while( t-- ) 38 { 39 cin >> n >> m; 40 for(int i = 1; i <= m; ++i) 41 cin >> a[i].x >> a[i].y; 42 for(int i = 0; i <= n; ++i) 43 cun[i] = i; 44 int cnt = 0; 45 for(int i = 1; i <= m; ++i) 46 { 47 if(find(a[i].x) != find(a[i].y)) 48 { 49 cnt++; 50 shu(a[i].x,a[i].y); 51 } 52 } 53 cout << n - cnt << endl; 54 } 55 return 0; 56 }
hdu 1213 How Many Tables(并查集),布布扣,bubuko.com
标签:des style blog http color os io for
原文地址:http://www.cnblogs.com/Unico-liyang/p/3894584.html