Rochambeau(带权并查集)

Rochambeau
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 3180 Accepted: 1109
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
Source


Baidu Star 2006 Preliminary 
Chen, Shixi (xreborner) living in http://fairyair.yeah.net/




按照食物链那题的做法,定 0,1,2 做为三种手势就可以了,不过由于判断是哪个人比较麻烦,不知道哪个人的选择是无效的,就不知道那几次游戏是不正确不能加入并查集的,因此就直接暴力枚举每个人是否为特殊的人,对于涉及这个人的所有游戏都不进行并查集操作,看剩下的游戏中是否会有矛盾,如果有就说明这个人不是特殊的人,否则他就可能是。最后如果只有一个人可能是,就可以判读出来,而判断出来的局数,就是枚举其他所有人出现矛盾的游戏场次的最大值,因为只有否定完其他所有人才能确定这个人是特殊的。



#include<stdio.h>
#include<string.h>
const int maxm=505;

int fa[maxm],num[maxm],wa[maxm];
struct ques{
    int a,b,c;
}q[2005];

void init(int n){
    for(int i=0;i<=n;++i){
        fa[i]=i;
        num[i]=0;
    }
}

int find(int x){
    int r=x,t1,t2,c=0;
    while(r!=fa[r]){
        c+=num[r];
        r=fa[r];
    }
    while(x!=r){
        t1=fa[x];
        t2=c-num[x];
        num[x]=c%3;
        fa[x]=r;
        c=t2;
        x=t1;
    }
    return r;
}

int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        for(int i=1;i<=m;++i){
            char c;
            scanf("%d",&q[i].a);
            c=getchar();
            while(c!='='&&c!='<'&&c!='>')c=getchar();
            if(c=='=')q[i].c=0;
            else if(c=='<')q[i].c=1;
            else if(c=='>')q[i].c=2;
            scanf("%d",&q[i].b);
        }
        memset(wa,-1,sizeof(wa));
        for(int i=0;i<n;++i){
            init(n);
            for(int j=1;j<=m;++j){
                if(q[j].a==i||q[j].b==i)continue;
                int x=find(q[j].a),y=find(q[j].b);
                if(x!=y){
                    fa[x]=y;
                    num[x]=((num[q[j].b]+q[j].c-num[q[j].a])%3+3)%3;
                }
                else{
                    if((num[q[j].b]+q[j].c)%3!=num[q[j].a]){wa[i]=j;break;}
                }
            }
        }
        int cnt=0,ans1,ans2=0;
        for(int i=0;i<n;++i){
            if(wa[i]==-1){
                cnt++;
                ans1=i;
            }
            if(wa[i]>ans2)ans2=wa[i];
        }
        if(!cnt)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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值