题意:
有n个人玩剪刀石头布,其中有一个人是裁判,剩下的人被分为三组,每组每次出的手型一样,裁判可以任意出,问是否能判断出哪个人为裁判。
题解:
枚举每个人为裁判的情况,然后问题就转化为了种类并查集问题(食物链),判断离了这个人,数据是否存在矛盾,如果不矛盾,说明这个人为裁判。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<string>
#include<cstring>
#include<vector>
#include<functional>
#include<utility>
#include<set>
#include<map>
#include<cmath>using namespace std;const int maxn=510;
int pa[maxn];
int d[maxn];int a[2005],b[2005],c[2005];
int findset(int x)
{int root;if(pa[x]==-1)return x;else{root=findset(pa[x]);d[x]=(d[x]+d[pa[x]])%3;return pa[x]=root;}
}bool unionset(int x,int y, int l)
{int rootx=findset(x);int rooty=findset(y);if(rootx==rooty){if((d[x]-d[y]+3)%3!=l)return false;return true;}else{pa[rootx]=rooty;d[rootx]=(d[y]-d[x]+l+3)%3;return true;}}int main()
{int n,m;while(scanf("%d%d",&n,&m)==2){ char tmp;for(int i=0;i<m;i++){//scanf("%d%c%d",&a[i],&c,&b[i]);cin>>a[i]>>tmp>>b[i];if(tmp=='=')c[i]=0;else if(tmp=='>')c[i]=1;else c[i]=2;}int cnt=0,id=0,round=-1;for(int i=0;i<n;i++){memset(pa,-1,sizeof(pa));memset(d,0,sizeof(d));bool flag=false;for(int j=0;j<m;j++){if(a[j]!=i&&b[j]!=i){if(!unionset(a[j],b[j],c[j]))//矛盾,说明不是裁判{flag=true;round=max(round,j);//记录判定了多少个回合。break;}}}if(!flag){id=i;cnt++;}//不矛盾(裁判)。cnt记录不矛盾的个数。}if(cnt==0)puts("Impossible");//没有裁判。else if(cnt==1)printf("Player %d can be determined to be the judge after %d lines\n",id,round+1);else puts("Can not determine");//不止一个裁判}
}