Title Link: http://poj.org/problem?id=2912
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1751
Test instructions: There are n people playing with stone scissors cloth, so will produce stone > scissors, scissors > cloth, Cloth > stone, so it has the same relationship with the food chain;
Enumeration + relationship and check set, enumerate each child as judge, if each round is correct in the current enumeration, then the current enumeration number may be judge.
If only one judge is found, the output will be. If there are more than one is not sure;
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<stack>#include<map>#include<vector>using namespaceStd;typedefLong LongLL;#defineN 2520#defineMet (A, b) memset (A, B, sizeof (a))intF[n], r[n], wrong[n];intFind (intx) { intK =F[x]; if(x!=F[x]) {F[x]=Find (f[x]); R[X]= (R[x]+r[k])%3; } returnf[x];}structnode{intx, Y, op;} A[n];intMain () {intN, M; while(SCANF ("%d%d", &n, &m)! =EOF) {Met (A,0); for(intI=1; i<=m; i++) { Charch; scanf ("%d%c%d", &a[i].x, &ch, &a[i].y); if(ch = ='=') A[i].op =0; if(ch = ='>') A[i].op =1; if(ch = ='<') A[i].op =2; } for(intI=0; i<n; i++)///enumeration of referees; { for(intj=0; j<n; J + +) F[j]= J, R[j] =0; Wrong[i]=0;///When I was a referee, where did the contradiction arise; for(intj=1; j<=m; J + +) { if(a[j].x = = I | | a[j].y = = i)Continue; intx = a[j].x, y =a[j].y; intPX =Find (x); intPY =Find (y); if(px! =py) {F[PX]=py; R[PX]= (R[y] + a[j].op-r[x] +3)%3; } Else if(px = = py && (r[y]+a[j].op)%3!=R[x]) {Wrong[i]=J; Break; } } } intJudge =0, ans =0, Index; for(intI=0; i<n; i++) { if(Wrong[i] = =0) {Judge++; Index=i; } ans= Max (Wrong[i], ans);///when the other situation is contradictory, then is the time to determine the result; } if(Judge = =1) printf ("Player%d can be determined to is the judge after%d lines\n", Index, ans); Else if(Judge = =0) printf ("impossible\n");///not produced; Elseprintf"Can not determine\n");///produce more than one, is not sure; } return 0;}
View Code
Rochambeau---poj2912| | zoj2751 (and a collection similar to the food chain)