标签:des style blog http io color ar os sp
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9204 Accepted Submission(s): 2961
题意:
1喜欢2,2喜欢3,而1又喜欢3,则矛盾。
基础并查集的新应用:
分组并查集(偏移量):开一个并查集,但是要加个偏移向量数组,来记录每个节点距离根节点的距离
代码:
1 /*带权值的并查集*/ 2 #include<cstdio> 3 #define maxn 2005 4 int father[maxn],rank[maxn]; 5 int tt,nn,mm; 6 void init() 7 { 8 for(int i=1;i<nn;i++){ 9 father[i]=i; 10 rank[i]=0; 11 } 12 } 13 14 int fin(int x) 15 { 16 if(x==father[x]) return x; 17 int temp=fin(father[x]); 18 rank[x]=(rank[x]+rank[father[x]])%2; 19 father[x]=temp; 20 return father[x]; 21 } 22 int unin(int a,int b) 23 { 24 int x=fin(a); 25 int y=fin(b); 26 if(x==y) 27 { 28 if(rank[a]==rank[b])return 1; //说明矛盾 29 return 0; 30 } 31 father[x]=y; 32 rank[x]=(rank[a]+rank[b]+1)%2; 33 return 0; 34 } 35 int main() 36 { 37 int a,b; 38 int tag; 39 scanf("%d",&tt); 40 for(int j=1;j<=tt;j++) 41 { 42 tag=0; 43 scanf("%d%d",&nn,&mm); 44 init(); 45 for(int i=0;i<mm;i++) 46 { 47 scanf("%d%d",&a,&b); 48 tag+=unin(a,b); 49 } 50 printf("Scenario #%d:\n",j); 51 52 if(tag)puts("Suspicious bugs found!"); 53 else puts("No suspicious bugs found!"); 54 puts(""); 55 } 56 return 0; 57 }
hdu 1829 A Bug's Life(分组并查集(偏移量))
标签:des style blog http io color ar os sp
原文地址:http://www.cnblogs.com/gongxijun/p/4100834.html