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
2 3 3 1 2 2 3 1 3 4 2 1 2 3 4
Sample Output
Scenario #1: Suspicious bugs found! Scenario #2: No suspicious bugs found!
Hint
Huge input,scanf is recommended.
Source
题意:
1喜欢2,2喜欢3,而1又喜欢3,则矛盾。 (排出同性恋)
基础并查集的新应用:
分组并查集(偏移量):开一个并查集,但是要加个偏移向量数组,来记录每个节点距离根节点的距离
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 }