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:\nSuspicious bugs found!\n\n",i);
51                 }
52             }
53         }
54         if(flag)
55             printf("Scenario #%d:\nNo suspicious bugs found!\n\n",i);
56     }
57     return 0;
58 }
View Code

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

转载于:https://www.cnblogs.com/yalphait/p/9662908.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值