POJ-2492 A Bug's Life

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

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.
题目大意:找到是否存在同性恋的情况。。。做的时候想到的是DFS,赛后发现题解是用并查集写的。
果然更优化了。
 
DFS思路:这个思路相对于并查集更容易想到,就是从根节点往其他根节点递归,用1表示雄性,2表示雌性,这样递归下去。
     如果没有成环那么就是肯定无bug,如果成的是环是偶数也没关系,关键就是环是奇数的时候存在bug。
     具体代码分析如下。
耗时938ms,内存开销24.3MB
果然用递归卡内存啊。。。
 
 
并查集思路:ps:思路来源:http://blog.csdn.net/svitter/article/details/38294329

开一个N*2的数组,前N表示雄性,后N表示雌性。

%d %d分别为i, j 

那么 i 和 j 一定不属于同一集合,i+n和j+n一定也不属于同一集合。

如果属于同一集合,那么出现bug。

DFS

AC代码:

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<vector>
 4 #include<string.h>
 5 #include<algorithm>
 6 using namespace std;
 7 int flag[1000050];
 8 vector<int> vec[1000050];//创建邻接表
 9 bool dfs(int now,int fa,int sex){
10     flag[now]=sex;
11     bool fgg=false;
12     for(int i=0;i<vec[now].size();i++){
13         int x=vec[now][i];
14         if(flag[x]){
15             if(flag[x]!=sex) continue;//判断是否为同性
16             else
17                 return true;
18         }
19         else{
20             if(x!=fa)//向子节点查询
21             {
22                 if(sex==1){       //当前节点为雄性的情况
23                     fgg=dfs(x,now,2);
24                 }else{               //当前节点为雌性的情况
25                     fgg=dfs(x,now,1);
26                 }
27                 if(fgg){
28                     return true;
29                 }
30             }
31         }
32     }
33     return false;
34 }
35 int main(){
36     int T,t=1;
37     cin>>T;
38     while(T--){
39         int n,q,x,y;
40         scanf("%d%d",&n,&q);
41         memset(flag,0,sizeof(flag));
42         for(int i=1;i<=q;i++){
43             scanf("%d%d",&x,&y);
44             vec[x].push_back(y);
45             vec[y].push_back(x);
46         }
47         bool fg=false;
48         for(int i=1;i<=n;i++){
49             if(!flag[i]){//判断是否查询过这个点
50                 flag[i]=1;
51                 fg=dfs(i,i,1);
52                 if(fg) break;
53             }
54         }
55         cout<<"Scenario #"<<t++<<':'<<endl;
56         if(fg){
57             cout<<"Suspicious bugs found!"<<endl;
58         }else{
59             cout<<"No suspicious bugs found!"<<endl;
60         }
61         cout<<endl;
62         for(int i=1;i<=n;i++){
63             vec[i].clear();
64         }
65     }
66     return 0;
67 }

并查集代码:ps:来源http://blog.csdn.net/svitter/article/details/38294329

 1 #include <iostream>  
 2 #include <stdio.h>  
 3 #include <string.h>  
 4 #include <vector>  
 5 #include <map>  
 6 #include <algorithm>  
 7 #include <queue>  
 8 #include <cmath>  
 9   
10 #define INF 0xffffff  
11   
12 using namespace std;  
13   
14 const int MAXN = 2000011;   
15 int root[MAXN];  
16 int t;  
17 int n, m;  
18   
19 void init()  
20 {  
21     for(int i = 0; i <= n*2; i++)  
22         root[i] = i;  
23 }  
24   
25 int getRoot(int i)  
26 {  
27     if(i == root[i])  
28         return i;  
29     return root[i] = getRoot(root[i]);  
30 }  
31   
32 void Merge(int i, int j)  
33 {  
34     int a = getRoot(i);  
35     int b = getRoot(j);  
36     if(a == b)  
37         return;  
38     root[a] = b;  
39 }  
40   
41 //judge same set  
42 bool jud(int i, int j)  
43 {  
44     return getRoot(i) == getRoot(j);  
45 }  
46   
47 int main()  
48 {  
49     int i, j;  
50     int t1, t2;  
51     bool flag;  
52     cin >> t;  
53     for(i = 1; i <= t; i++)  
54     {  
55         flag = false;  
56   
57         //input  
58         scanf("%d%d", &n, &m);  
59         init();  
60         for(j = 0; j < m; j++)  
61         {  
62             scanf("%d%d",&t1, &t2);  
63             if(flag)  
64                 continue;  
65             if(jud(t1, t2) || jud(t1+n, t2+n))  
66                 flag = 1;  
67             else  
68             {  
69                 Merge(t1, t2+n);  
70                 Merge(t1+n, t2);  
71             }  
72         }  
73   
74         //result  
75         printf("Scenario #%d:
", i);  
76         if(flag)  
77             printf("Suspicious bugs found!
");  
78         else  
79             printf("No suspicious bugs found!
");  
80         if(i != t)  
81             printf("
");  
82     }  
83   
84     return 0;  
85 }  
原文地址:https://www.cnblogs.com/ISGuXing/p/7269160.html