首页 > 代码库 > hdu 1829 A Bug's Life(分组并查集(偏移量))
hdu 1829 A Bug's Life(分组并查集(偏移量))
A Bug‘s Life
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9204 Accepted Submission(s): 2961
Problem Description
Background
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.
Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.
Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.
Input
The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.
Output
The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs‘ sexual behavior, or "Suspicious bugs found!" if Professor Hopper‘s assumption is definitely wrong.
Sample Input
23 31 22 31 34 21 23 4
Sample Output
Scenario #1:Suspicious bugs found!Scenario #2:No suspicious bugs found!
Hint
Huge input,scanf is recommended.
Source
TUD Programming Contest 2005, Darmstadt, Germany
题意:
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(分组并查集(偏移量))
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。