Question Link
AC code
#include<iostream>
#include<cstdio>
using namespace std;
#define N 505
#define M 2005
int pre[N],dis[N],a[M],b[M];
char op[M];
int find(int x)
{
if(x==pre[x]) return x;
int t=pre[x];
pre[x]=find(pre[x]);
dis[x]=(dis[x]+dis[t])%3;
return pre[x];
}
void Union(int x,int y,int fx,int fy,int w)
{
pre[fy]=fx;
dis[fy]=(dis[x]-dis[y]+w+3)%3;//這公式,我只能說是別人推出來,我枚舉驗證了一邊
}
int main()
{
int i,j,n,m;
while(~scanf("%d%d",&n,&m)){
for(i=1;i<=m;i++){
scanf("%d%c%d",&a[i],&op[i],&b[i]);
if(op[i]=='=') op[i]=0;
if(op[i]=='>') op[i]=1;
if(op[i]=='<') op[i]=2;
}
int judge=-1,round=0;
bool only=true;
for(i=0;i<n;i++){
bool f=true;
for(j=0;j<n;j++){
pre[j]=j;
dis[j]=0;
}
for(j=1;j<=m;j++){
if(a[j]==i||b[j]==i) continue;
int fx=find(a[j]),fy=find(b[j]);
if(fx==fy){
if(dis[b[j]]!=(dis[a[j]]+op[j])%3){
f=false;
round=max(round,j);
break;
}
}
else{
Union(a[j],b[j],fx,fy,op[j]);
}
}
if(f){
if(judge==-1)
judge=i;
else{
only=false;
break;
}
}
}
if(judge==-1) puts("Impossible");
else{
if(!only) puts("Can not determine");
else printf("Player %d can be determined to be the judge after %d lines\n",judge,round);
}
}
return 0;
}