Description
N children are playing Rochambeau (scissors-rock-cloth) game with you. One of them is the judge. The rest children are divided into three groups (it is possible that some group is empty). You don’t know who is the judge, or how the children are grouped. Then the children start playing Rochambeau game for M rounds. Each round two children are arbitrarily selected to play Rochambeau for one once, and you will be told the outcome while not knowing which gesture the children presented. It is known that the children in the same group would present the same gesture (hence, two children in the same group always get draw when playing) and different groups for different gestures. The judge would present gesture randomly each time, hence no one knows what gesture the judge would present. Can you guess who is the judge after after the game ends? If you can, after how many rounds can you find out the judge at the earliest?
Input
Input contains multiple test cases. Each test case starts with two integers N and M (1 ≤ N ≤ 500, 0 ≤ M ≤ 2,000) in one line, which are the number of children and the number of rounds. Following are M lines, each line contains two integers in [0, N) separated by one symbol. The two integers are the IDs of the two children selected to play Rochambeau for this round. The symbol may be “=”, “>” or “<”, referring to a draw, that first child wins and that second child wins respectively.
Output
There is only one line for each test case. If the judge can be found, print the ID of the judge, and the least number of rounds after which the judge can be uniquely determined. If the judge can not be found, or the outcomes of the Mrounds of game are inconsistent, print the corresponding message.
Sample Input
3 3 0<1 1<2 2<0 3 5 0<1 0>1 1<2 1>2 0<2 4 4 0<1 0>1 2<3 2>3 1 0
Sample Output
Can not determine Player 1 can be determined to be the judge after 4 lines Impossible Player 0 can be determined to be the judge after 0 lines
思路:
食物链的小变化,枚举1-n,如果去掉该小盆友在的情况,剩下情况全部成立,即该小盆友是裁判,
如果没一个成立,说明存在多个裁判,即cnt=0输出不可能。
如果存在多个情况成立,表明条件不充分,输出不能确定。
代码:
#include<stdio.h>
#define N 505
int f[N],group[N],cnt;
struct node
{
int a,b,c;
}data[2005];
void getdata(int m)
{
int i;
char c;
for(i=0;i<m;i++)
{
scanf("%d %c %d",&data[i].a,&c,&data[i].b);
if(c=='=')data[i].c=0;
else if(c=='<')data[i].c=1;
else if(c=='>')data[i].c=2;
}
}
void init(int n)
{
int i;
for(i=0;i<n;i++)f[i]=i,group[i]=0;
}
int getf(int a)
{
if(f[a]==a)return a;
int t=getf(f[a]);
group[a]=(group[a]+group[f[a]])%3;
return f[a]=t;
}
void merge(int a,int b,int fa,int fb,int c)
{
f[fb]=fa;
group[fb]=(3+group[a]+c-group[b]);
}
int judge(int a,int b,int c)
{
if((c+group[a])%3==group[b])return 0;
return 1;
}
int main()
{
int i,j,n,m,fa,fb,ans1,ans2;
while(~scanf("%d%d",&n,&m))
{
ans2=0;
cnt=0;
getdata(m);//数据读入
for(i=0;i<n;i++)
{
init(n);
for(j=0;j<m;j++)
{
if(i==data[j].a||i==data[j].b)continue;
fa=getf(data[j].a);
fb=getf(data[j].b);
if(fa!=fb)
merge(data[j].a,data[j].b,fa,fb,data[j].c);
else if(judge(data[j].a,data[j].b,data[j].c))
{
if(j+1>ans2)
ans2=j+1;
break;
}
}
if(j==m)
{
cnt++;
ans1=i;
}
}
if(cnt==0)printf("Impossible\n");
else if(cnt>1)printf("Can not determine\n");
else printf("Player %d can be determined to be the judge after %d lines\n",ans1,ans2);
}
return 0;
}