题意:有t组测试数据, 对于每组数据,第一行n, m分别表示昆虫的数目和接下来m行x, y,x, y表示教授判断x, y为异性, 问教授是否有错误判断,即存在x, y为同性;
种类并查集 0 表示同性, 1表示异性
链接:poj 2492
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <vector>
#include <sstream>
#define ll long long
using namespace std;
const int maxn = 2000 + 10;
const int maxm = 1000000 + 10;
const int inf = 0x3f3f3f3f;
int father[maxn];
int r[maxn];
int n, m, k;
void init() {
for(int i = 0; i <= n; i++) {
father[i] = i;
r[i] = 0;
}
}
int getf(int x) {
int t;
if(father[x] != x) {
t = getf(father[x]);
r[x] = (r[x] + r[father[x]]) % 2;
return father[x] = t;
}
return x;
}
int main()
{
int kcase = 0;
int T;
scanf("%d", &T);
while(T--) {
scanf("%d %d", &n, &m);
init();
int flag = 1;
while(m--) {
int a, b;
scanf("%d %d", &a, &b);
int x = getf(a), y = getf(b);
if(x == y) {
if((r[a] + r[b]) % 2 == 0) {
flag = 0;
}
}
else {
father[x] = y;
r[x] = (r[a] + r[b] + 1) % 2;
}
}
if(kcase) puts("");
printf("Scenario #%d:\n", ++kcase);
if(flag) puts("No suspicious bugs found!");
else puts("Suspicious bugs found!");
}
return 0;
}