poj 2912 Rochambeau

链接:

http://poj.org/problem?id=2912


题目:

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

Source




题目大意:

n个人玩,玩石头剪刀布游戏,其中1人是裁判,剩下的n-1个人分为3组, 他们商量好了,相同组的人每次都出相同的手势,不同组的人是不同的,而裁判是随机出的。给出m个结果,判断那个是裁判。


分析与总结:

这题昨天纠结了一个晚上没AC, 今天又搞了一个晚上敲打。。

关键是依次枚举每一个人,这个很难想到,设枚举到的这个人为裁判,然后对其他不包括裁判的那些进行判断,判断的方法和食物链那题一样。如果没有矛盾,那么说明这个人是裁判。

最终,看有多少个人可能是裁判,如果0个,说明是 Impossible, 1个说明找到符合的唯一裁判,超过1个说明是Can not determine。

如果符合的话,那么怎样判断是从哪一步开始可以确定呢? 答案是除了裁判之外的其他枚举时,出现矛盾步数最大的那个就是能确定裁判的步数。





代码:

#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;

const int N = 510, M = 2005;
int n,m,f[N],rank[N],cnt, time[N];
int A[M], B[M];
char C[M];

inline void init(){
    for(int i=0; i<=n; ++i)
        f[i]=i, rank[i]=0;
}
int find(int x){
    if(x==f[x]) return x;
    int fa=f[x];
    f[x] = find(f[x]);
    rank[x] = (rank[x]+rank[fa])%3;
    return f[x];
}
inline bool Union(int x,int y,int d){
    int a=find(x), b=find(y);
    if(a==b){
        if(d==0&&rank[x]!=rank[y]) return false;
        if(d==1){
            if((rank[x]+1)%3!=rank[y]) return false;
        }
        return true;
    }
    f[b] = a;
    rank[b] = (rank[x]-rank[y]+3+d)%3;
    return true;
}

int main(){
    int a,b;
    char ch;
    while(~scanf("%d%d",&n,&m)){
        for(int i=0; i<m; ++i){
            scanf("%d",&A[i]);
            scanf("%c",&C[i]);
            // 防止有空格
            while(C[i]==' ') scanf("%c",&C[i]);
            scanf("%d",&B[i]);
        }
        int cnt=0, cur=0, person=0;  
        memset(time, 0, sizeof(time));
		// 枚举每个judge
        for(int i=0; i<n; ++i){
            init();
            bool flag=false;
            for(int j=0; j<m; ++j)if(A[j]!=i && B[j]!=i){
                if(C[j]=='=' && !Union(A[j],B[j],0)){
                    flag=true; 
                    if(j>time[i]) { time[i]=j+1; }
                    break; 
                }
                if(C[j]=='<' && !Union(A[j],B[j],1)){
                    flag=true; 
                    if(j>time[i]) { time[i]=j+1; }
                    break; 
                }
                if(C[j]=='>' && !Union(B[j],A[j],1)){ 
                    flag=true; 
                    if(j>time[i]) { time[i]=j+1; }
                    break; 
                }
            }
            if(!flag){
                person = i;
                ++cnt;
            }
        }
        if(cnt==0) 
            puts("Impossible");
        else if(cnt==1){
            for(int i=0; i<n; ++i)if(i!=person&&time[i]>cur) cur=time[i];
            printf("Player %d can be determined to be the judge after %d lines\n",person, cur);
        }
        else 
            puts("Can not determine");
    }
    return 0;
}




——  生命的意义,在于赋予它意义。

          
     原创 http://blog.csdn.net/shuangde800 , By   D_Double  (转载请标明)



  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值