题目链接:http://poj.org/problem?id=2912
和食物链那题差不多,稍微复杂一点。
枚举裁判,然后其他人判断是否冲突。
1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 const int maxn=520; 5 const int maxe=2010; 6 int f[maxn],r[maxn],a[maxe],b[maxe]; 7 char op[maxe]; 8 int n,m; 9 void init() 10 { 11 for(int i=0;i<n;i++){ 12 f[i]=i; 13 r[i]=0; 14 } 15 } 16 17 int gf(int x) 18 { 19 if(x!=f[x]) 20 { 21 int t=f[x]; 22 f[x]=gf(t); 23 r[x]=(r[x]+r[t])%3; 24 } 25 return f[x]; 26 } 27 28 int main() 29 { 30 while(scanf("%d%d",&n,&m)!=EOF) 31 { 32 for(int i=0;i<m;i++) 33 { 34 scanf("%d%c%d",&a[i],&op[i],&b[i]); 35 if(op[i]=='=') op[i]=0; 36 if(op[i]=='>') op[i]=1; 37 if(op[i]=='<') op[i]=2; 38 } 39 int only=1,judge=-1,round=0; 40 for(int i=0;i<n;i++) 41 { 42 init(); 43 int ok=1; 44 for(int j=0;j<m;j++) 45 { 46 if(a[j]==i||b[j]==i) continue; 47 int aa=a[j],bb=b[j],opp=op[j]; 48 int pa=gf(aa); 49 int pb=gf(bb); 50 if(pa==pb) 51 { 52 if(r[aa]!=(r[bb]-opp+3)%3) 53 { 54 ok=0; 55 round=max(round,j+1); //记录第几轮证明出不是裁判 56 break; 57 } 58 } 59 else { 60 f[pb]=pa; 61 r[pb]=(r[aa]+opp-r[bb]+3)%3; 62 } 63 } 64 if(ok) 65 { 66 if(judge==-1) judge=i; 67 else {only=0;break;} 68 } 69 } 70 if(judge==-1) puts("Impossible"); 71 else if(only==0) puts("Can not determine"); 72 else printf("Player %d can be determined to be the judge after %d lines\n",judge,round); 73 74 } 75 return 0; 76 }