POJ 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

题意:有n个人在玩剪刀石头布,其中有个人是裁判(他可以任意出剪刀石头布的一种),现在有m对两两对决的结果,判断能否在这m对结果中找到这个裁判(找到的条件当然是在有一个裁判的情况下前后结果不矛盾)。那么会产生4种结果:1.n==1,即只有一个人,那么他肯定是裁判,输出 Player 0 can be determined to be the judge after 0 lines;2.这m个结果完全没有矛盾,或者有矛盾,但是有好几人都满足裁判的条件,那么就不能判断谁是裁判了,输出 Can not determine;3.这n个人无论是谁当裁判都有矛盾,那么这种情况是不可能找到裁判的,输出 Impossible;4.满足一切情况,那么就输出 Player a can be determined to be the judge after b lines,a代表裁判的序号,b代表最早到第几行能够判断裁判是a。
#include<stdio.h>
#include<algorithm>
using namespace std;

const int N=510;

struct node
{
    int a, b, c; ///a,b是人的序号,c是a,b之间的关系
}no[N*20];
int f[N], r[N];

int Find(int x)
{
    int k = f[x];

    if (f[x] != x)
    {
        f[x] = Find(f[x]);
        r[x] = (r[x]+r[k])%3;
    }

    return f[x];
}

int Solve(int k, int n, int m) ///若是k当裁判,判断是否有矛盾,和食物链差不多
{
    int i, a, b, c, na, nb;

    for (i = 0; i < n+10; i++)
    {
        f[i] = i;
        r[i] = 0;
    }

    for (i = 0; i < m; i++)
    {
        a = no[i].a; b = no[i].b; c = no[i].c;

        if (a != k && b != k) ///因为裁判是可以任意出的,所以判断是否矛盾时要跳过裁判本身
        {
            na = Find(a); nb = Find(b);

            if (na == nb && (c+r[b])%3 != r[a]) ///出现矛盾,返回i,那么在输出最早行时就可以输出i+1了
                return i; 

            f[na] = nb;
            r[na] = ((c-r[a])+r[b]+3)%3;
        }
    }

    return -1;
}

int main ()
{
    int n, m, i, p, q, s, t;
    char ch;

    while (scanf("%d%d", &n, &m) != EOF)
    {
        s = 0;

        for (i = 0; i < m; i++)
        {
            scanf("%d%c%d", &no[i].a, &ch, &no[i].b);

            if (ch == ‘>‘) no[i].c = 2; ///我们可以令>时关系为2,<时关系为1,=时关系为0(0,1,2,,,好像食物链)
            else if (ch == ‘<‘) no[i].c = 1;
            else no[i].c = 0;
        }

        p = Solve(-1, n, m); ///第一次让-1当裁判(实际上没这个人,主要是要看这m个结果中是否有矛盾)

        if (n == 1) printf("Player 0 can be determined to be the judge after 0 lines\n"); ///只有1个人,没办法,自己是裁判
        else if (p == -1 && n != 1) printf("Can not determine\n"); ///m个结果没有任何矛盾,那么就无法判断谁是裁判(好像谁都能当裁判似的)
        else
        {
            t = p; ///先保存一下产生矛盾的地方

            for (i = 0; i < n; i++)
            {
                p = Solve(i, n, m);

                if (p == -1)
                {
                    q = i; ///当i裁判时没有矛盾,要记录下来,说不定他就是真正的裁判
                    s++;
                }
                else t = max(t, p); ///如果想最早找到裁判,就得是其他人当裁判产生矛盾最远的地方
                if (s > 1) break;
            }

            if (s == 1) printf("Player %d can be determined to be the judge after %d lines\n", q, t+1);
            else if (s > 1) printf("Can not determine\n");
            else printf("Impossible\n");
        }
    }

    return 0;
}
时间: 2024-10-21 16:58:22

POJ 2912 Rochambeau的相关文章

poj 2912 Rochambeau(带权并查集 + 暴力)

题目:poj 2912 Rochambeau(带权并查集 + 暴力) 题目大意:题目给出三个团队和一个裁判,这三个团队和裁判一起玩剪刀石头布,然后规定每个团队必须出一样的,只有裁判可以任意出.然后给出关系,x > y 代表 x 赢y , x < y代表 y 赢 x , 相等则出的一样.问这样的关系可以推出裁判是哪个吗?可以需要说明从第一条到第几条推出来的,不可以也要说明是不可能出现这样的关系,还是裁判不唯一. 解题思路:这题重点是裁判在里面会扰乱关系,并且n * m 才 100000,完全可以

poj 2912 Rochambeau(枚举+带权并查集)

题目链接:http://poj.org/problem?id=2912 题意:多个人玩石头剪刀布分成3组和一个裁判,每一组提前选定了自己出哪个手势,裁判可以随意出什么手势,问是否能够从给出的一系列石头剪刀布游戏中判断出哪个是裁判的,可以从第几局游戏中判断出来. 由于这题的数据量比较小可以枚举一下,枚举一下每一个人假设他们是裁判然后一系列并查集下来看 有没有出现矛盾如果没有那么这个人可能是裁判候补,如果有矛盾那么这个人肯定不是记录一下出现 问题的位置然后继续枚举.位置要去最远的,因为都要判完才知道

poj 2912 Rochambeau【枚举+种类并查集】

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

poj 2912 并查集(食物链加强版)

题目:给出n个人玩剪刀石头布的游戏,其中有一个人是裁判,剩下的人分为3组,每一组的人只出某一种手型,裁判可以任意出.问是否能判断出哪个人是裁判 链接:点我 分分钟看吐血,先把食物链看懂吧 枚举裁判,然后并查集判断 裁判由于可以任意出,所以可能属于任意一个集合,所以有裁判参与的会合不考虑,然后并查集部分和食物链很相似. 如果某个裁判那里出现了矛盾,则记录一下在哪出问题. 然后判断是否只有一个裁判没有出现问题.如果只有一个,说明可以确定,那么就是剩下的人出问题的最大值.因为只有否定了其它所有人,才能

poj 2912(带权并查集)

题意:有n个人玩石头剪刀布的游戏,编号从1到n-1,把所有人分成3组,给每个组分配一个手势(石头.剪子.布),每轮挑出两个人出来进行游戏,这n个人里有一个裁判,他的手势是可以变化的.给出了m轮的游戏结果,a < b表示b赢了a,a = b表示a和b出了同样的手势,问能否找到裁判的编号,是在第几轮发现的. 题解:这题和食物链那个经典带权并查集很像,也可以用0表示父与子是同一种手势,用1表示父大于子,用2表示父小于子.因为裁判的编号不能确定,可以采用枚举的方式,先假定一个人是裁判,然后去掉这个人所有

*并查集的题*

POJ 1182 食物链http://acm.pku.edu.cn/JudgeOnline/problem?id=1182题目告诉有3种动物,互相吃与被吃,现在告诉你m句话,其中有真有假,叫你判断假的个数(如果前面没有与当前话冲突的,即认为其为真话)这题有几种做法,我以前的做法是每个集合(或者称为子树,说集合的编号相当于子树的根结点,一个概念)中的元素都各自分为A, B, C三类,在合并时更改根结点的种类,其他点相应更改偏移量.但这种方法公式很难推,特别是偏移量很容易计算错误.下面来介绍一种通用

PyDev的安装(编写Python的Eclipse插件)

题目:poj 2912 Rochambeau(带权并查集 + 暴力) 题目大意:题目给出三个团队和一个裁判,这三个团队和裁判一起玩剪刀石头布,然后规定每个团队必须出一样的,只有裁判可以任意出.然后给出关系,x > y 代表 x 赢y , x < y代表 y 赢 x , 相等则出的一样.问这样的关系可以推出裁判是哪个吗?可以需要说明从第一条到第几条推出来的,不可以也要说明是不可能出现这样的关系,还是裁判不唯一. 解题思路:这题重点是裁判在里面会扰乱关系,并且n * m 才 100000,完全可以

跟着chengyulala刷题之[kuangbin带你飞]之&#39;并查集&#39;专题/斜眼笑

[kuangbin带你飞] 专题1-23 https://vjudge.net/article/187 专题五 并查集 POJ 2236 Wireless Network  http://poj.org/problem?id=2236POJ 1611 The Suspects  http://poj.org/problem?id=1611HDU 1213 How Many Tables  http://acm.hdu.edu.cn/showproblem.php?pid=1213HDU 3038

POJ2912 Rochambeau [扩展域并查集]

题目传送门 Rochambeau Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4463   Accepted: 1545 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