您的位置:首页 > Web前端

poj 2492 A Bug's Life (并查集)

2014-02-17 20:34 429 查看
题目:http://poj.org/problem?id=2492

题意:跟上一道1703题差不做,

给出m对昆虫交配,问 有没有同性恋。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = 2010;
int bin[maxn], op[maxn], n;
void init()
{
for(int i = 1; i <= n; i++)
{
bin[i] = i;
op[i] = 0;
}
}
int find(int x)
{
int r, i, j;
r =  x;
while(r != bin[r])
r = bin[r];
i = x;
while(bin[i] != r)
{
j = bin[i];
bin[i] = r;
i = j;
}
return r;
}

void merge(int x, int y)
{
int fx, fy;
fx = find(x);
fy = find(y);
if(fx != fy)
bin[fx] = fy;
}
int main()
{
int t, m, a, b, f, cou=1;
scanf("%d", &t);
while(t--)
{
f = 0;
scanf("%d%d", &n, &m);
init();

while(m--)
{
scanf("%d%d",&a, &b);
if(find(a) == find(b))
f = 1;
if(f == 0)
{
if(op[a] == 0)
op[a] = b;
else
merge(op[a],b);
if(op[b] == 0)
op[b] =a;
else
merge(op[b],a);
}
}
printf("Scenario #%d:\n", cou++);
if(f)
printf("Suspicious bugs found!\n\n");
else
printf("No suspicious bugs found!\n\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: