poj2492——A Bug's Life(并查集扩展)

Address:http://poj.org/problem?id=2492

此题今天我们写的也很纠结。最后才发现是初始化 放错位置了。

#include<iostream>
#include<cstdio>
using namespace std;
#define MAX 2010
int father[MAX],rela[MAX];
int find(int x)
{
	if(x!=father[x])
	{
		int temp=father[x];
		father[x]=find(father[x]);
		rela[x]=(rela[x]+rela[temp])%2;
	}
	return father[x];
}
int main()
{
	int T,i,j,m,n,x,y;
	scanf("%d",&T);
	for(i=1;i<=T;i++)
	{
		scanf("%d%d",&n,&m);
		bool flag=false;
		for(j=1;j<=n;j++)
		{
			father[j]=j;
			rela[j]=0;
		}
		for(j=1;j<=m;j++)
		{
			int rx,ry;
			scanf("%d%d",&x,&y);
			rx=find(x);	ry=find(y);
			if(rx==ry&&rela[x]==rela[y])
				flag=true;
			else
			{
				father[rx]=ry;
				rela[rx]=(rela[x]-rela[y]+1)%2;
			}
		}
		if(flag)
			printf("Scenario #%d:\nSuspicious bugs found!\n\n",i);
		else
			printf("Scenario #%d:\nNo suspicious bugs found!\n\n",i);
	}
	return 0;
}


 

你可能感兴趣的:(poj2492——A Bug's Life(并查集扩展))