A Bug's Life
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14981 Accepted Submission(s): 4868
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.
Source
TUD Programming Contest 2005, Darmstadt, Germany
Recommend
linle
Statistic |
Submit |
Discuss |
Note
题意:n个虫子,m个关系,你可以假设任意虫子是男是女,判断是否有同性恋
大致想了一下,哪个是男是女都无所谓,带权并查集,和食物链那道题很像,原理不再多说了
传送门http://blog.csdn.net/m0_37134257/article/details/60876110,感觉写的很详细了;
判断与根节点的关系;
如果两者的根节点相同,那么就判断,两者是否都是男或都是女;
如果不相同,合并根节点,并更新根节点之间的关系,以0,1判断性别,相同即为同性恋,所以要对2取余
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
#include<stdlib.h>
#include<queue>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
using namespace std;
int64_t n,m,flag;
int64_t f[2005],r[2005];
int64_t getf(int64_t x)
{
if(f[x]==x)return x;
else
{
int64_t t=getf(f[x]);
r[x]=(r[x]+r[f[x]])%2;
f[x]=t;
return f[x];
}
}
void mix(int64_t x,int64_t y)
{
int64_t tx=getf(x);
int64_t ty=getf(y);
if(tx!=ty)
{
f[tx]=ty;
r[tx]=(1+r[y]-r[x])%2;
}
else
{
if(r[x]==r[y])
flag=1;
}
}
void init()
{
for(int64_t i=1;i<=n;i++)
{
f[i]=i;
r[i]=0;
}
flag=0;
}
int main()
{
int64_t t,q=1,a,b;
scanf("%I64d",&t);
while(t--)
{
scanf("%I64d%I64d",&n,&m);
init();
for(int i=0;i<m;i++)
{
scanf("%I64d%I64d",&a,&b);
mix(a,b);
}
printf("Scenario #%I64d:\n",q++);
if(flag==1)
printf("Suspicious bugs found!\n\n");
else
printf("No suspicious bugs found!\n\n");
}
return 0;
}
转载请注明原文地址: https://ju.6miu.com/read-670062.html