标签:identify win bool ane fat mem life name des
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14121 Accepted Submission(s): 4603
1 //将相同性别的bugs放进同一并查集里,这样只要输入的两个bug有相同的根就是gay. 2 //路径压缩的while 的find比递归的find稍快一些。 3 #include<iostream> 4 #include<cstdio> 5 #include<cstring> 6 using namespace std; 7 int t,n,m; 8 int fat[2005],sex[2005]; 9 int find(int x) 10 { 11 int rt=x; 12 while(fat[rt]!=rt) 13 rt=fat[rt]; 14 int i=x,j; 15 while(i!=rt) 16 { 17 j=fat[i]; 18 fat[i]=rt; 19 i=j; 20 } 21 return rt; 22 } 23 /*int find(int x) 24 { 25 if(fat[x]!=x) 26 fat[x]=find(fat[x]); 27 return fat[x]; 28 }*/ 29 void connect(int x,int y) 30 { 31 int xx=find(x),yy=find(y); 32 if(xx!=yy) 33 fat[xx]=yy; 34 } 35 int main() 36 { 37 int a,b; 38 scanf("%d",&t); 39 for(int k=1;k<=t;k++) 40 { 41 scanf("%d%d",&n,&m); 42 for(int i=0;i<=n;i++) 43 { 44 fat[i]=i; 45 sex[i]=0; 46 } 47 bool flag=0; 48 for(int i=1;i<=m;i++) 49 { 50 scanf("%d%d",&a,&b); 51 if(flag) continue; 52 if(find(a)==find(b)) 53 { 54 flag=1; 55 continue; 56 } 57 if(sex[a]==0) sex[a]=b; 58 else connect(sex[a],b); 59 if(sex[b]==0) sex[b]=a; 60 else connect(sex[b],a); 61 } 62 printf("Scenario #%d:\n",k); 63 if(flag) printf("Suspicious bugs found!\n"); 64 else printf("No suspicious bugs found!\n"); 65 printf("\n"); 66 } 67 return 0; 68 }
标签:identify win bool ane fat mem life name des
原文地址:http://www.cnblogs.com/--ZHIYUAN/p/6013686.html