1. 程式人生 > >Rochambeau[擴充套件域並查集]

Rochambeau[擴充套件域並查集]

傳送門

將一個點拆成3個,分別表示x_scissors , x_rock , x_cloth

如果x>y 那麼表示x是scissors y就是cloth , x是rock y就是scissors , x是cloth y就是cloth

合併3個就可以了

我們是通過排除法來確定裁判編號的,列舉每個編號為裁判,一但在某一行輸入中出現矛盾,我們就確定它不是裁判,那麼n-1個出現矛盾的列舉中出現矛盾最晚的那個行數即為我們能確定裁判所需的最少行數啦(因為我們要排除n-1個編號才能確定裁判的編號嘛)~ ----https://www.cnblogs.com/geloutingyu/p/6145706.html


#include<cstdio>
#define N 505*3
#define M 2005
#define xx find(x)
#define x1 find(x+n)
#define x2 find(x+n+n)
#define yy find(y)
#define y1 find(y+n)
#define y2 find(y+n+n)
using namespace std;
int fa[N],n,m,cnt,Max,pos;
struct Node{int x,y,op;}a[M];
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
int check(int now){
	for(int i=1;i<=m;i++){
		int x=a[i].x,y=a[i].y,op=a[i].op;
		if(x==now || y==now) continue;
		else{
			if(op==1){
				if(xx!=y1) fa[xx]=y1;
				if(x1!=y2) fa[x1]=y2;
				if(x2!=yy) fa[x2]=yy;
			}
			if(op==2){
				if(xx!=yy) fa[xx]=yy;
				if(x1!=y1) fa[x1]=y1;
				if(x2!=y2) fa[x2]=y2;
			}
			if(op==3){
				if(xx!=y2) fa[xx]=y2;
				if(x1!=yy) fa[x1]=yy;
				if(x2!=y1) fa[x2]=y1;
			}
			if(xx==x1||xx==x2||x1==x2||yy==y1||y1==y2||yy==y2) return i;
		}
	}return 0;
}
int main(){
	while(~scanf("%d%d",&n,&m)){
		cnt=Max=pos=0; 
		for(int i=1;i<=m;i++){
			int x,y; char ch[3]; 
			scanf("%d%c%d",&x,&ch,&y);
			a[i].x=x+1 , a[i].y=y+1;
			if(ch[0]=='<') a[i].op=1;
			if(ch[0]=='=') a[i].op=2;
			if(ch[0]=='>') a[i].op=3;
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n*3;j++) fa[j]=j;
			int x=check(i);
			if(!x) cnt++,pos=i;
			else if(x>Max) Max=x;
		}
		if(cnt==0) printf("Impossible\n");
		if(cnt==1) printf("Player %d can be determined to be the judge after %d lines\n",pos-1,Max);
		if(cnt>1) printf("Can not determine\n");
	}
}