Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 41165 | Accepted: 13419 |
Description
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
Output
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
Source
TUD Programming Contest 2005, Darmstadt, Germany
题意:输入一个T代表T组数据,每组输入一个n,m表示有n只虫子m对情侣,问你是否存在矛盾(及一个雄的既和雌的是情侣又和雄的是情侣)
思路:可以用搜索记录一下这只虫子的性别,出现矛盾就结束
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 2010
#define M 1000010
typedef struct
{
int v,next;
}Node;
Node e[M*2];
int vis[N],head[M];
int n,m,tot,flag;
void add(int x,int y)
{
e[tot].v=y;
e[tot].next=head[x];
head[x]=tot++;
}
void dfs(int x,int y)
{
int i,j,k;
for(i=head[x];i!=-1;i=e[i].next)
{
j=e[i].v;
if(vis[j]==-1)
{
vis[j]=(y+1)%2;
dfs(j,(y+1)%2);
}
else
if(vis[j]==y)
{
flag=0;
return;
}
}
}
int main()
{
int t,i,j,k,ca=1,x,y;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
tot=0;
memset(head,-1,sizeof(head));
memset(vis,-1,sizeof(vis));
for(i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
flag=1;
for(i=1;i<=n&&flag;i++)
{
if(vis[i]==-1)
{
vis[i]=0;
dfs(i,0);
}
}
printf("Scenario #%d:\n",ca++);
if(flag)
printf("No suspicious bugs found!\n");
else
printf("Suspicious bugs found!\n");
if(t)
printf("\n");
}
return 0;
}