A Bug's Life
TimeLimit: 15000/5000 MS (Java/Others) Memory Limit:32768/32768 K (Java/Others)
Total Submission(s): 18391 Accepted Submission(s): 5873
ProblemDescription
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 interactwith bugs of the opposite gender. In his experiment, individual bugs and theirinteractions were easy to identify, because numbers were printed on theirbacks.
Problem
Given a list of bug interactions, decide whether the experiment supports hisassumption of two genders with no homosexual bugs or if it contains some buginteractions that falsify it.
Input
The first line of the input contains thenumber of scenarios. Each scenario starts with one line giving the number ofbugs (at least one, and up to 2000) and the number of interactions (up to1000000) separated by a single space. In the following lines, each interactionis 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 linecontaining "Scenario #i:", where i is the number of the scenariostarting at 1, followed by one line saying either "No suspicious bugsfound!" if the experiment is consistent with his assumption about thebugs' sexual behavior, or "Suspicious bugs found!" if ProfessorHopper's assumption is definitely wrong.
SampleInput
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
SampleOutput
Scenario#1:
Suspiciousbugs found!
Scenario#2:
Nosuspicious bugs found!
Hint
Huge input,scanf is recommended.
Source
TUDProgramming Contest 2005, Darmstadt, Germany
算法分析:
题意:
3 3
1 2
2 3
1 3
1和2是异性,2和3是异性,1和3又是异性。矛盾。
第一种算法:
多设一个数组r[],r[i]=j:表示i和j为异性,然后每次的并都要把回互相的异性并上,这样就可以把其分为两组了。(具体实现看代码)
第二种算法:
1—2—3 每个点对应0 1 0 ……..如果相邻点为相同数则有问题。
代码实现:
#include<bits/stdc++.h>
using namespace std;
#define N 2005
int fa[N],r[N];//r[i]=j:表示i和j为异性
int find(int x)
{
return fa[x]!=x?(fa[x]=find(fa[x])):x;
}
int main()
{
int n,m;
int T;
scanf("%d",&T);
int c=0;
while(T--)
{
c++;
scanf("%d%d",&n,&m);
int flag=0;
for(int i=0;i<=n;i++)
{fa[i]=i;r[i]=0;}
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
if(flag) continue;
int r1=find(x),r2=find(y);
if(r1!=r2)
{
if(r[r1]) fa[r[r1]]=r2;
if(r[r2]) fa[r[r2]]=r1;
r[r1]=r2;
r[r2]=r1;
//这样就可以把异性分成两个组了
}
else
{
flag=1;
}
}
printf("Scenario #%d:\n",c);
if(flag)printf("Suspicious bugs found!\n");
else printf("No suspicious bugs found!\n");
printf("\n");
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define N 2005
int fa[N],r[N];
int find(int x)
{
if(fa[x]==x)
return x;
int t=find(fa[x]);
r[x]=(r[x]+r[fa[x]])&1;//相当于%2,
fa[x]=t;
return t;
}
int main()
{
int n,m;
int T;
scanf("%d",&T);
int c=0;
while(T--)
{ c++;
scanf("%d%d",&n,&m);
int flag=0;
for(int i=1;i<=n;i++)
{fa[i]=i;r[i]=0;}
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
if(flag) continue;
int r1=find(x),r2=find(y);
if(r1!=r2)
{
fa[r1]=r2;
r[r1]=(r[x]+r[y]+1)&1;
}
else
{
if(r[x]==r[y])
flag=1;
}
}
printf("Scenario #%d:\n",c);
if(flag)printf("Suspicious bugs found!\n");
else printf("No suspicious bugs found!\n");
printf("\n");
}
return 0;
}