首页 > 代码库 > *HDU1829 并查集

*HDU1829 并查集

A Bug‘s Life

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14121    Accepted Submission(s): 4603


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.
 

 

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!
 
 
题意:
有若干bugs,一对一对的给出,判断其中是否有同性恋,如果1与2,2与3,1又与3,就说明1和3是gay.
代码:
 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 }

 

*HDU1829 并查集