poj 2492

Posted 见字如面

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 2492相关的知识,希望对你有一定的参考价值。

 
                                                                           

A Bug‘s Life

POJ - 2492
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!

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<map>
 8 #include<set>
 9 #include<vector>
10 #include<cstdlib>
11 #include<string>
12 #define eps 0.000000001
13 typedef long long ll;
14 typedef unsigned long long LL;
15 using namespace std;
16 const int N=2000+100;
17 int flag[N];
18 int parent[N];
19 int m,n;
20 void init(){
21     for(int i=0;i<=n;i++)parent[i]=i;
22     memset(flag,0,sizeof(flag));
23 }
24 int find(int x){
25     if(parent[x]!=x){
26         int root=parent[x];
27         parent[x]=find(parent[x]);
28         flag[x]=(flag[x]+flag[root])%2;
29     }
30     return parent[x];
31 }
32 /*
33 int find(int x){
34     int r=x;
35     while(r!=parent[r])r=parent[r];
36     int i=x;
37     int j;
38     while(i!=r){
39         flag[i]=(flag[i]+flag[parent[i]])%2;
40         j=parent[i];
41         parent[i]=r;
42         i=j;
43     }
44     return r;
45 }*/
46 void Union(int x,int y){
47     int xx=find(x);
48     int yy=find(y);
49     parent[xx]=yy;
50     flag[xx]=(flag[y]+flag[x]+1)%2;
51 }
52 int main()
53 {
54     //freopen("text.txt","r",stdin);
55     int T,kase;
56     scanf("%d",&T);
57     for(int kase=1;kase<=T;kase++)
58     {
59         scanf("%d%d",&n,&m);
60         init();
61         int flag1=0;
62         for(int i=0;i<m;i++)
63         {
64             int x,y;
65             scanf("%d%d",&x,&y);
66             if(flag1)
67                 continue;
68             int fx=find(x);
69             int fy=find(y);
70             if(fx==fy)
71             {
72                 if(flag[x]==flag[y])
73                     flag1=1;
74             }
75             else
76                 Union(x,y);
77         }
78         printf("Scenario #%d:\n",kase);
79         if(flag1)
80             printf("Suspicious bugs found!\n");
81         else
82             printf("No suspicious bugs found!\n");
83         if(kase!=T)
84             printf("\n");
85     }
86     return 0;
87 }

 

以上是关于poj 2492的主要内容,如果未能解决你的问题,请参考以下文章

poj2492(种类并查集)

poj 2492

poj 2492

POJ 2492(权值向量并查集)

A Bug's Life POJ - 2492 (带权并查集)

A Bug's Life POJ - 2492