【基础种类并查集】A Bug's Life
A Bug's Life
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11804 Accepted Submission(s): 3839
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
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!
解法:
基础的种类并查集,如果i为男的话,则i+N为女,反之亦然。
用并查集连接,同一集合的表示相同的性别,有两个大集合分别表示男和女的集合。
如果在下次查找关系找到a和b是已经存在同一个集合的话,说明他们两个在之前的判断是为同性,则说明存在gay。
反之,如果a和b没有存在同一个集合中的话,则表示a和b+n,b和a+n为同性,分别更新两个大集合、
代码:2015.8.7
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 #include <math.h> 5 #define MAX 2000100 6 using namespace std; 7 int ID[MAX]; 8 void Cread(int N) 9 { 10 for(int i=1;i<=N;i++)ID[i]=i; 11 } 12 int Find(int x) 13 { 14 if(ID[x]!=x)ID[x]=Find(ID[x]); 15 return ID[x]; 16 } 17 int Add_(int a,int b) 18 { 19 int A=Find(a); 20 int B=Find(b); 21 if(A!=B){ID[A]=B;return 0;} 22 else return 1; 23 } 24 int main() 25 { 26 int T,N,M,a,b,Sign,t=1; 27 scanf("%d",&T); 28 while(T--) 29 { 30 scanf("%d%d",&N,&M); 31 Cread(2*N);Sign=0; 32 while(M--) 33 { 34 scanf("%d%d",&a,&b); 35 if(Find(a)==Find(b)){Sign=1;continue;} 36 {Add_(a,b+N);Add_(b,a+N);} 37 } 38 printf("Scenario #%d:\n",t++); 39 if(Sign)printf("Suspicious bugs found!\n"); 40 else printf("No suspicious bugs found!\n"); 41 putchar(10); 42 } 43 return 0; 44 }
转载请备注:
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************