A Bug's Life 并查集,很好的应用,假定同性的放入一个集合中,矛盾时输出结果;...

博客围绕教授对虫子性别行为的研究展开,假设虫子只有两种性别且只与异性互动。通过给定虫子互动列表,利用并查集算法判断实验是否支持该假设。给出了输入输出格式及示例,还提供了相应的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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!
***************************************************************************************************************************
***************************************************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 using namespace std;
 7 int fa[2005];
 8 int com[2005];
 9 int i,j,k,n,m,cas;
10 void init()
11 {
12     for(i=1;i<=2005;i++)
13      fa[i]=i;
14     memset(com,-1,sizeof(com));
15 }
16 int find(int x)
17  {
18      if(x==fa[x])
19        return fa[x];
20      return fa[x]=find(fa[x]);
21  }
22 void Unon(int x,int y)
23  {
24     int xt=find(x);
25     int yt=find(y);
26      if(xt!=yt)
27       fa[xt]=yt;
28  }
29 int main()
30 {
31     scanf("%d",&cas);
32     k=0;
33     while(cas--)
34     {
35         ++k;
36         init();
37         int st,en;
38         scanf("%d%d",&n,&m);
39         int found=0;
40         for(i=1;i<=m;i++)
41         {
42             scanf("%d%d",&st,&en);
43             if(com[st]!=-1)
44             {
45                 if(com[en]!=-1)
46                 {
47                     if(find(st)==find(en))
48                     {
49                         found=1;
50 
51                     }
52                     Unon(en,com[st]);
53                     Unon(st,com[en]);
54                      continue;
55                 }
56                 Unon(com[st],en);
57                 com[en]=st;
58             }
59             else
60             {
61                 if(com[en]!=-1)
62                 {
63                     Unon(com[en],st);
64                     com[st]=en;
65                 }
66                 else
67                 {
68                     com[st]=en;
69                     com[en]=st;
70                 }
71             }
72         }
73         printf("Scenario #%d:\n",k);
74         if(found==1)
75           printf("Suspicious bugs found!\n");
76         else
77           printf("No suspicious bugs found!\n");
78         printf("\n");
79 
80     }
81     return 0;
82 
83 }
View Code

 

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3375114.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值