您的位置:首页 > Web前端

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

2013-06-17 19:21 477 查看
题目链接

居然没更新flag就交了,错了两次。。3个题,都是一个类型。

#include <cstring>
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int o[5001];
int flag[5001];
int find(int x)
{
if(x == o[x]) return x;
int t = find(o[x]);
flag[x] = (flag[x] + flag[o[x]])%2;
return o[x] = t;
}
int main()
{
int n,m,i,T,cas = 1,x,y,tx,ty,z;
scanf("%d",&T);
while(cas <= T)
{
scanf("%d%d",&n,&m);
for(i = 1; i <= n; i ++)
{
o[i] = i;
flag[i] = 0;
}
z = 1;
for(i = 0; i < m; i ++)
{
scanf("%d%d",&x,&y);
if(z)
{
tx = find(x);
ty = find(y);
if(tx != ty)
{
o[tx] = ty;
flag[tx] = (flag[y] - flag[x] + 3)%2;
}
else if(tx == ty)
{
if(flag[x] == flag[y])
z = 0;
}
}
}
printf("Scenario #%d:\n",cas++);
if(z)
printf("No suspicious bugs found!\n");
else
printf("Suspicious bugs found!\n");
if(cas <= T) printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: