Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

uva10158(War)并查集

$
0
0
#include<iostream>
using namespace std;
int f[20005];
int n;
int find(int x)
{
	return f[x]==x?x:f[x]=find(f[x]);
}
int areFriends(int x,int y)
{
	if(find(x)!=find(y)&&find(x)!=find(y+n)) return -1;
	if(find(x)==find(y)) return 1;
	if(find(x)!=find(y)) return 0;
}
int areEnemies(int x,int y)
{
	if(find(x)!=find(y)&&find(x)!=find(y+n)) return -1;
	if(find(x)!=find(y)) return 1;
	if(find(x)==find(y)) return 0;
}
int main()
{
	
	int t,x,y;
	cin>>n;
	for(int i=1;i<=2*n;i++)
		f[i]=i;
	cin>>t>>x>>y;
	while(t||x||y)
	{
		if(t==1) 
		{
			if(areFriends(x,y)==0) cout<<"-1"<<endl;
			if(areFriends(x,y)==-1)
			{
				f[find(x)]=find(y);
				f[find(x+n)]=find(y+n);
			}

		}
		if(t==2)
		{
			if(areEnemies(x,y)==0) cout<<"-1"<<endl;
			if(areEnemies(x,y)==-1)
			{
				f[find(x+n)]=find(y);
				f[find(x)]=find(y+n);
			}
		}
		if(t==3)
		{
			if(areFriends(x,y)==1) cout<<"1"<<endl;
			else cout<<"0"<<endl;
		}
		if(t==4)
		{
			if(areEnemies(x,y)==1) cout<<"1"<<endl;
			else cout<<"0"<<endl;
		}
		cin>>t>>x>>y;
	}
	return 0;
}

作者:highacm 发表于2013-4-18 8:10:42 原文链接
阅读:59 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>