poj 2912(kmp+暴力)

Rochambeau
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 4470   Accepted: 1547

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 M rounds 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

kmp找个体数,暴力找哪个是裁判。
 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 const int maxn=506;
 5 int fa[maxn],val[maxn];
 6 struct node{
 7     int a,b,w;
 8 }seek[2006];
 9 
10 void init()
11 {
12     for(int i=0;i<maxn;i++){
13         fa[i]=i;
14         val[i]=0;
15     }
16 }
17 
18 int find_fa(int x)
19 {
20     if(fa[x]==x) return x;
21     int tmp=fa[x];
22     fa[x]=find_fa(tmp);
23     val[x]=(val[x]+val[tmp])%3;
24     return fa[x];
25 }
26 
27 int main()
28 {
29     int n,m;
30     while( ~scanf("%d%d",&n,&m)){
31         for(int i=1;i<=m;i++){
32             int x,y;
33             char mid;
34             scanf("%d%c%d",&seek[i].a,&mid,&seek[i].b);
35             if(mid=='<') seek[i].w=1;
36             if(mid=='>') seek[i].w=2;
37             if(mid=='=') seek[i].w=0;
38         }
39         int ans,num=0,way=0;
40         for(int i=0;i<n;i++){
41             init();
42             int flag=0;
43             for(int j=1;j<=m;j++){
44                 if(seek[j].a==i||seek[j].b==i)
45                     continue;
46                 int x,y,tmp;
47                 x=seek[j].a; y=seek[j].b;
48                 tmp=seek[j].w;
49                 int opx=find_fa(x);
50                 int opy=find_fa(y);
51                 if(opx!=opy){
52                     fa[opy]=opx;
53                     val[opy]=(3+tmp+val[x]-val[y])%3;
54                 }
55                 if(opx==opy&&(3+(-val[x]+val[y]))%3!=tmp){
56                     flag=1;
57                     if(j>way) way=j;
58                     break;
59                 }
60             }
61             if(!flag){
62                 num++;
63                 ans=i;
64             }
65         }
66 
67         if(num==0)
68             printf("Impossible\n");
69         else if(num>1)
70             printf("Can not determine\n");
71         else printf("Player %d can be determined to be the judge after %d lines\n",ans,way);
72     }
73     return 0;
74 }

 


posted @ 2018-05-30 16:09  flyer_duck  阅读(136)  评论(0编辑  收藏  举报