HDU 1829 - A Bug's Life

Posted

tags:

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

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:\n",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!\n");
27         else  puts("Suspicious bugs found!\n");
28     } return 0;
29 }

 

以上是关于HDU 1829 - A Bug's Life的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1829 A Bug's Life (种类并查集)

hdu 1829 &amp;poj 2492 A Bug&#39;s Life(推断二分图带权并查集)

hdu1829A Bug's Life(种类并查集)

hdoj 1829 A bug's life 种类并查集

带权并查集(含种类并查集)经典模板 例题:①POJ 1182 食物链(经典)②HDU - 1829 A bug's life(简单) ③hihoCoder 1515 : 分数调查(示例代码(代

HDU1289 A Bug's Life (带权并查集)