POJ 2912 Rochambeau

参考

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n, m, fa[1505], pla1[2005], pla2[2005], winn[2005], det[505], cntn, maxn,whic;
char opt;
int myfind(int x){
	return fa[x]==x?x:fa[x]=myfind(fa[x]);
}
int chk(int x){
	for(int i=0; i<3*n; i++)	fa[i] = i;
	for(int i=1; i<=m; i++){
		if(pla1[i]==x || pla2[i]==x)	continue;
		int uu1=myfind(pla1[i]), uu2=myfind(pla1[i]+n), uu3=myfind(pla1[i]+n+n);
		int vv1=myfind(pla2[i]), vv2=myfind(pla2[i]+n), vv3=myfind(pla2[i]+n+n);
		if(winn[i]==1){
			if(uu1==vv1 || uu2==vv1)	return i;
			fa[vv2] = uu1;
			fa[vv3] = uu2;
			fa[vv1] = uu3;
		}
		else if(!winn[i]){
			if(uu1==vv2 || uu2==vv1)	return i;
			fa[vv1] = uu1;
			fa[vv2] = uu2;
			fa[vv3] = uu3;
		}
		else{
			if(uu1==vv1 || uu1==vv2)	return i;
			fa[vv1] = uu2;
			fa[vv2] = uu3;
			fa[vv3] = uu1;
		}
	}
	return 0;
}
int main(){
	while(scanf("%d %d", &n, &m)!=EOF){
		for(int i=1; i<=m; i++){
			scanf("%d%c%d", &pla1[i], &opt, &pla2[i]);
			if(opt=='=')	winn[i] = 0;
			else	winn[i] = opt=='>'?1:-1;
		}
		for(int i=0; i<n; i++)	det[i] = chk(i);
		cntn = maxn = 0;
		for(int i=0; i<n; i++){
			if(!det[i])	cntn++, whic=i;
			maxn = max(maxn, det[i]);
		}
		if(!cntn)	printf("Impossible
");
		else if(cntn>1)	printf("Can not determine
");
		else	printf("Player %d can be determined to be the judge after %d lines
", whic, maxn);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/poorpool/p/8489514.html