题目意思:
给出N条虫子,然后a和b交配,给出M对a和b后问有没有同性恋的虫子
思路:
并查集:
把相同性别的虫子连到一起,看是否有矛盾
CODE
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
int ls[20010];
int f[20010];
int fid(int x)
{
return x == f[x] ?x:fid(f[x]);
}
void bc(int x,int y)
{
int xx = fid(x);
int yy = fid(y);
if(xx!=yy)
f[xx] = yy;
}
int main()
{
int t;
scanf("%d",&t);
int ki = 1;
while(t--)
{
int n, m;
scanf("%d%d",&n,&m);
memset(ls,0,sizeof(ls));
for(int i = 0; i <= n; i ++)
f[i] = i;
int flag = 0;
for(int i = 1; i <= m; i++)
{
int a, b;
scanf("%d%d",&a,&b);
if(!ls[a]&&!ls[b])
{
ls[a] = b;
ls[b] = a;
}
else if(!ls[a]&&ls[b])
{
ls[a] = b;
bc(a,ls[b]);
}
else if(ls[a]&&!ls[b])
{
ls[b] = a;
bc(b,ls[a]);
}
else
{
bc(ls[a],b);
bc(ls[b],a);
}
if(fid(a) == fid(b))
flag = 1;
}
printf("Scenario #%d:\n",ki++);
if(flag)
{
printf("Suspicious bugs found!\n");
}
else
{
printf("No suspicious bugs found!\n");
}
printf("\n");
}
return 0;
}