HDU 1829

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!
Hint
Huge input,scanf is recommended.
 
 
大致题意:
  检查一堆数据中是否有同性恋,找出主要矛盾是如果1交配2,2交配3,而1又交配3,则矛盾。找到一组就行。
解题思路:
  用并查集做元素关系判断,主要建立余数体系;
 
 1 #include <cstdio>
 2 using namespace std;
 3 int f[2000+5],num[2000+5],t,n,m,a,b,fa,fb;
 4 int sf(int x){
 5     if(x==f[x]) return x;
 6     else {
 7         int fx=sf(f[x]);
 8         num[x]=(num[x]+num[f[x]])%2;
 9         return f[x]=fx;
10     }
11 }
12 int main(){
13     scanf("%d",&t);
14     for(int k=1;k<=t;k++){
15         printf("Scenario #%d:
",k);
16         scanf("%d%d",&n,&m);    bool flag=1;
17         for(int i=0;i<=n;i++) f[i]=i,num[i]=0;
18         for(int i=1;i<=m;i++){
19             scanf("%d%d",&a,&b);
20             if(flag){
21                 fa=sf(a); fb=sf(b); 
22                 if(fa==fb){ if((num[a]-num[b]+2)%2!=1) flag=0;}//避免负数 
23                 else f[fb]=fa,num[fb]=(num[a]-num[b]+1+2)%2;
24             }
25         }
26         if(flag) puts("No suspicious bugs found!
");
27         else  puts("Suspicious bugs found!
");
28     } return 0;
29 }
我自倾杯,君且随意
原文地址:https://www.cnblogs.com/nicetomeetu/p/5164380.html