A Bug's Life
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14050 Accepted Submission(s): 4572
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.
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!HintHuge input,scanf is recommended.
Source
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829
题意:判断是否存在同性恋。
样例1:1和2是异性,2和3也是异性,但1 和3也是异性那就不可能了,其中必定存在同性恋。
参考博客:http://blog.csdn.net/shuangde800/article/details/7974664
http://www.cnblogs.com/Shirlies/archive/2012/03/05/2380144.html
http://blog.csdn.net/creat2012/article/details/20037747
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=2000+5;
int n,m;
int fa[maxn];
int sex[maxn];
bool flag;
void Init()
{
flag=false;
for(int i=0; i<=n; i++)
fa[i]=i,sex[i]=0;
}
int Find(int x)
{
if(x!=fa[x])
{
int fx=fa[x];
fa[x]=Find(fa[x]);
sex[x]=(sex[fx]+sex[x])&1;
}
return fa[x];
/**
if(x==fa[x])
return fa[x];
int t=Find(fa[x]);
sex[x] = (sex[fa[x]]+sex[x])&1;
fa[x]=t;
return fa[x];
*/
}
void Union(int x,int y)
{
int fx=Find(x);
int fy=Find(y);
if(fx==fy)
{
if(sex[x]==sex[y])
flag=true;
return ;
}
fa[fx]=fy;
sex[fx]=(sex[x]+sex[y]+1)&1;
}
int main()
{
int T;
int kase=0;
cin>>T;
while(T--)
{
cin>>n>>m;
Init();
int x,y;
while(m--)
{
scanf("%d%d",&x,&y);
if(flag) continue;
Union(x,y);
}
printf("Scenario #%d:\n",++kase);
if(flag)
printf("Suspicious bugs found!\n\n");
else
printf("No suspicious bugs found!\n\n");
}
return 0;
}

本文解析了一道名为ABug'sLife的算法题目,该题目要求通过一系列虫子之间的交互来判断是否所有虫子都遵循特定的行为模式。文章提供了详细的解题思路及完整的AC代码实现。

被折叠的 条评论
为什么被折叠?



