标签:style blog io color os for sp div 问题
在算法上松懈了好久,决心开始慢慢捡起来了。一开始先跟初学一样吧。基础一点,也能发现以前自己的问题。
1 #include<iostream> 2 #include<cstdio> 3 #define MAXN 1111 4 int TestCase = 0; 5 int N,M; 6 int tr[MAXN]; 7 int tr_find(int x) 8 { 9 while(tr[x] != x) 10 x = tr[x]; 11 return x; 12 } 13 void tr_merge(int a,int b) 14 { 15 int ra = tr_find(a); 16 int rb = tr_find(b); 17 if(ra!=rb) 18 tr[ra] = rb; 19 return; 20 } 21 int main() 22 { 23 // freopen("in.txt","r",stdin); 24 scanf("%d",&TestCase); 25 while(TestCase --) { 26 scanf("%d%d",&N,&M); 27 for(int i = 1; i <= N; i++) 28 tr[i] = i; 29 while(M--) { 30 int a,b; 31 scanf("%d%d",&a,&b); 32 tr_merge(a,b); 33 } 34 int s = 0; 35 for(int i = 1; i <= N; i++) 36 // std::cout<<" "<<tr[i]; 37 if(tr[i] == i) 38 ++s; 39 printf("%d\n",s); 40 } 41 }
标签:style blog io color os for sp div 问题
原文地址:http://www.cnblogs.com/takeoffyoung/p/4055718.html