Rochambeau (POJ - 2912 )

Rochambeau (POJ - 2912 )

这道题我在3个月前一发a了,但今天比赛却一直wa。。。。。,后来发现是初始化应该每一次循环进行一次,但我却把它放在循环之外,导致一直wa!!!!。。。

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

题目翻译:

孩子们正在和你玩剪刀石头布的游戏。其中一个是法官。其余的孩子被分成三组(可能有些组是空的)。你不知道谁是裁判,也不知道孩子们是如何分组的。然后孩子们开始玩罗尚博游戏M轮。每轮随机选择两名儿童玩一次罗尚博游戏,游戏结果会告诉你,但你不知道孩子们做了什么手势。我们知道,同一组的孩子会做出相同的手势(因此,同一组的两个孩子在玩耍时总是会被画图),不同的组会做出不同的手势。法官每次都会随机做出手势,所以没有人知道法官会做出什么手势。你能猜出比赛结束后谁是裁判吗?如果可以的话,几轮之后你能尽早找到裁判?

输入

输入包含多个测试用例。每个测试用例以两个整数N和M(1≤N≤500,0≤M≤2000)开始,在一行内,分别为子数和轮数。下面是M行,每一行包含两个整数在[0,N]之间由一个符号分隔。这两个整数是这轮中被选中扮演罗尚博的两个孩子的id。符号可以是“=”、“>”或“<”,表示抽签时,第一个孩子获胜,第二个孩子获胜。

输出

每个测试用例只有一行。如果可以找到法官,打印法官的ID,以及法官可以唯一确定的最少轮数。如果找不到裁判,或者M轮比赛结果不一致,打印相应的信息。

样例输入

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

样例输出

不能确定

玩家1可以在第4行后被判定为裁判

不可能的

玩家0可以在0行之后被判定为裁判

思路:因为这道题是让你求最少需要多少行可以确定法官,而你此时并不知道谁是法官,所以你去掉法官对结果没有影响,此时枚举一下每个人是法官的情况,然后暴力。

AC代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#define MAX 10010
using namespace std;
int pre[MAX],v[MAX];
int getf(int a)
{
    if(a==pre[a])
        return a;
    int tmp=getf(pre[a]);
    v[a]=(v[a]+v[pre[a]]+3)%3;
    pre[a]=tmp;
    return tmp;
}
int mer(int a, int b, int c)
{
    int fa=getf(a);
    int fb=getf(b);
    if(fa==fb)
    {
        if((v[b]-v[a]+3)%3!=c)
        {
            return 1;
        }
    }
    else
    {
        pre[fb]=fa;
        v[fb]=(v[a]-v[b]+c+3)%3;
    }
    return 0;
}
int main()
{
    int n, m;
    while(~scanf("%d%d",&n,&m))
    {
        int a[MAX], b[MAX],s;
        char c[MAX];
        for(int i=1; i<=m; i++)
            scanf("%d%c%d",&a[i],&c[i],&b[i]);
        int sum=0, flag=1, p=0, k=0;
        for(int i=0; i<n; i++)
        {
            flag=1;
            for(int j=0; j<n; j++)
            {
                v[j]=0;
                pre[j]=j;
            }
            for(int j=1; j<=m; j++)
            {
                if(a[j]==i || b[j]==i)
                {
                    continue;
                }
                if(c[j]=='=')
                {
                    s=0;
                }
                else if(c[j]=='>')
                {
                    s=1;
                }
                else
                {
                    s=2;
                }
                if(mer(a[j],b[j],s))
                {
                    p=max(j, p);
                    flag=0;
                    break;
                }
            }
            if(flag)
            {
                sum++;
                k=i;
            }
        }
        if(!sum)
        {
            printf("Impossible\n");
        }
        else if(sum>1)
        {
            printf("Can not determine\n");
        }
        else
        {
            printf("Player %d can be determined to be the judge after %d lines\n", k, p);
        }
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值