题意:
每行两个数字,表示两个虫子相互喜欢,只有异性才能相互喜欢,所以求里面有没有基佬或者拉拉= =。
思路:
种类并查集,只有两种状态,父节点与自身性别相同,父节点与自身性别不同。
更新并判断。
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std;
int M,N;
struct node{
int fa;
int p;
}F[3000];
void init()
{
for (int i = 0; i <= 3000; i++)
{
F[i].fa = i;
F[i].p = 0;
}
return ;
}
int find(int x)
{
if (x==F[x].fa) return x;
int t = F[x].fa;
F[x].fa = find(F[x].fa);
F[x].p ^= F[t].p;
return F[x].fa;
}
int main(void)
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
int T;
scanf("%d", &T);
for (int t = 1; t <= T; t++)
{
init();
scanf("%d %d", &N, &M);
int flag = 0;
while (M--)
{
int x, y;
scanf("%d %d", &x, &y);
int a = find(x);
int b = find(y);
if (a!=b)
{
F[a].fa = b;
F[a].p = (F[x].p-F[y].p+1)%2;
}
else
{
if (F[x].p == F[y].p)
flag = 1;
}
}
printf("Scenario #%d:\n",t);
printf("%s\n\n", flag==1?"Suspicious bugs found!":"No suspicious bugs found!");
}
return 0;
}