Rochambeau

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

标题:Rochambeau

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

题意:有几个小孩在做石头剪刀布游戏,他们当中有一个裁判,下面给出几组刚开始他们的胜负情况,让你判断谁是裁判,并输出最少的步数。

       若不能判断输出  Can not determine

       若没有输出    Impossible

       其他情况就输出谁在第几步被判断成为裁判

思路:用种类并查集存储各个人之间的关系,然后从0编号开始假设其为裁判,若其中有一步与前面冲突,则代表他不是裁判,依次类推,这道题关键是每次判断一个人是不是裁判时,需要初始化pre,与v数组(WA了n发,坑死了);

#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#define MAX 2010
using namespace std;
int v[MAX],pre[MAX];
int n,m;
char cc;
int a,b;
struct node
{
    int u,v,w;
} k[MAX];
void init()
{
    for(int i=0; i<=n; i++)
    {
        pre[i]=i;
        v[i]=0;
    }
}
int fa(int root)
{
    if(root==pre[root])
        return root;
    int tep=fa(pre[root]);
    v[root]=(v[root]+v[pre[root]]+3)%3;
    pre[root]=tep;
    return tep;
}
int judge(int aa,int bb,int c)
{
    int root1=fa(aa);
    int root2=fa(bb);
    if(root1==root2)
    {
        if((v[aa]-v[bb]+3)%3==c)
            return 1;
        else
            return 0;
    }
    else
    {
        pre[root1]=root2;
        v[root1]=(v[bb]+c-v[aa]+3)%3;
        return 1;
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1; i<=m; i++)
        {
            scanf("%d%c%d",&a,&cc,&b);
            k[i].u=a;
            k[i].v=b;
            if(cc=='=')
                k[i].w=0;
            else if(cc=='>')
                k[i].w=1;
            else
                k[i].w=2;
        }
        int kk,ans=0;
        int dis[MAX];
        memset(dis,0,sizeof(dis));
        for(int i=0; i<n; i++)
        {
            init();
            int flag=0;
            for(int j=1; j<=m; j++)
            {
                if(k[j].u==i||k[j].v==i)
                    continue;
                else
                {
                    if(!judge(k[j].u,k[j].v,k[j].w))
                    {
                        dis[i]=j;
                        flag=1;
                        break;
                    }
                }
            }
            if(!flag)
            {
                ans++;
                kk=i;
            }
        }
        if(ans==0)
            printf("Impossible\n");
        else if(ans>=2)
            printf("Can not determine\n");
        else
        {
            int maxx=-1;
            for(int i=0; i<n; i++)
                if(dis[i]>maxx)
                    maxx=dis[i];
            printf("Player %d can be determined to be the judge after %d lines\n",kk,maxx);
        }
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值