[poj2492]A Bug's Life(并查集+补集)

A Bug's Life
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 34678   Accepted: 11339

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!

Hint

Huge input,scanf is recommended.

Source

TUD Programming Contest 2005, Darmstadt, Germany
 
 
此题同poj1182 http://www.cnblogs.com/Pumbit-Legion/p/5925331.html
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<climits>
 6 #include<algorithm>
 7 #include<queue>
 8 #define LL long long
 9 int fa[4020],size[4020];
10 int n;
11 inline int read(){
12        int sum=0;char ch=getchar();
13        while(ch>'9'||ch<'0')ch=getchar();
14        while(ch<='9'&&ch>='0'){
15              sum=sum*10+ch-'0';
16              ch=getchar();
17        }
18        return sum;
19 }
20 inline int fnd(int x){
21     int r=x;
22     while(x!=fa[x])x=fa[x];
23         int tmp;
24     while(r!=x)tmp=fa[r],fa[r]=x,r=tmp;
25     return x;
26 }
27 inline int uni(int x,int y){
28     int fx=fnd(x);
29     int fy=fnd(y);
30     if(size[fx]<size[fy])fa[fx]=fy;
31     else if(size[fx]>size[fy])fa[fy]=fx;
32     else{
33         fa[fy]=fx;
34         ++size[fx];
35     }
36     return 0;
37 }
38 int main(){
39     int t,x=1;
40     t=read();
41     while(x<=t){
42         printf("Scenario #%d:\n",x);
43         int i,m;
44         n=read(),m=read();
45         memset(size,0,sizeof(size[0])*n*2+10);
46         for(i=1;i<=n*2;++i)fa[i]=i;
47         int note=0,a,b;
48         for(i=1;i<=m;++i){
49             a=read(),b=read();
50             if(!note){
51                int fa1=fnd(a);
52                int fb1=fnd(b);
53                if(fa1==fb1){
54                    note=1;
55                    puts("Suspicious bugs found!");
56                    continue;
57                }
58                int fa2=fnd(a+n);
59                int fb2=fnd(b+n);
60                uni(fa1,fb2);
61                uni(fa2,fb1);
62             }
63         }
64         if(!note)puts("No suspicious bugs found!");
65         puts("");
66         x++;
67     }
68     return 0;
69 }
View Code

 

posted @ 2016-10-01 01:16  Pumbit-Legion  阅读(185)  评论(0编辑  收藏  举报