18.9.17 poj2492 A Bug's Life

Posted yalphait

tags:

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

描述

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.输入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.输出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.样例输入

2
3 3
1 2
2 3
1 3
4 2
1 2
3 4

样例输出

Scenario #1:
Suspicious bugs found!

Scenario #2:
No suspicious bugs found!

提示

Huge input,scanf is recommended.

来源

TUD Programming Contest 2005, Darmstadt, Germany

技术分享图片
 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 const int maxn = 2005;
 6 int parent[maxn];//根节点
 7 int relation[maxn];//与根节点性别关系 0同一性别 1异性
 8 
 9 int findparent(int x){
10     if(parent[x]!=x){
11         int root=findparent(parent[x]);
12         relation[x]=(relation[x]+relation[parent[x]])%2;
13         parent[x]=root;
14     }
15     return parent[x];
16 }
17 
18 bool merge(int x,int y){
19     int px=findparent(x),py=findparent(y);
20     if(px==py){
21         if(relation[x]==relation[y])return false;
22         return true;
23     }
24     relation[py]=(relation[x]-relation[y]+1)%2;
25     parent[py]=px;
26     return true;
27 }
28 
29 int main()
30 {
31     int scene;
32     scanf("%d",&scene);
33     for(int i=1;i<=scene;i++)
34     {
35         bool flag=true;
36         int n, k;
37         scanf("%d%d",&n,&k);
38         for(int i=1;i<=n;i++)
39         {
40             relation[i]=0;
41             parent[i]=i;
42         }
43         while(k--){
44             int x,y;
45             scanf("%d%d",&x,&y);
46             if(flag==true)
47             {
48                 flag=merge(x,y);
49                 if(!flag){
50                     printf("Scenario #%d:
Suspicious bugs found!

",i);
51                 }
52             }
53         }
54         if(flag)
55             printf("Scenario #%d:
No suspicious bugs found!

",i);
56     }
57     return 0;
58 }
View Code

只是把食物链删改了一下,比前者要简单

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

POJ - 2492 A Bug's Life

POJ 2492 A Bug's Life

poj2492 A Bug's Life (并查集拓展)

POJ 2492 A Bug's Life

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

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