poj2912 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

题目大意:小朋友们玩石头剪刀布,决定了出什么之后就会一直出什么。除了裁判,裁判每次出的招式可能不一样。给出几组比试结果,求出谁是裁判。如果能求出则写出最少在几组比试结果之后就能得出结果。如果求不出,则给出不可能求得出或者是结果可能性太多无法判定。思路:并查集食物链的加强版。在求出父亲的同时,还增加了一个关系说明。a[x]表示x与其父亲fx的关系。1:fx<x。2:fx>x。3:fx=x。      每次求出父亲的时候还要计算他们之间的关系。      求裁判时,枚举谁是裁判。然后将裁判的出拳结果出数据中去掉,接下来开始求小朋友间的胜负关系是否矛盾。如果出了去除裁判k之外,去除其他都会得出矛盾,则裁判是k。如果去除多个数都没有矛盾,裁判无法判断。如果去除谁有矛盾,则不可能求出结果。
/*
 * Author:  Joshua
 * Created Time:  2014年07月12日 星期六 10时47分42秒
 * File Name: poj2912.cpp
 */
#include<cstdio>
#include<algorithm>
#include<cstring>
#define maxn 505
#define maxm 2005

using namespace std;
typedef long long LL;
int n,m;
int a[maxn],f[maxn];

int gf(int x)
{
    if (f[x]!=x)
    {
        int fx=f[x];
        f[x]=gf(f[x]);
        a[x]=(a[x]+a[fx])%3;
    }
    return f[x];
}

void solve()
{
    int x[maxm],y[maxm],p[maxm];
    int ans,err,step=0,gfx,gfy,type,xx,yy,cnt=0;
    char c;
    for (int i=1;i<=m;++i)
    {
        scanf("%d%c%d",&x[i],&c,&y[i]);
        if (c==‘<‘) p[i]=1;
        if (c==‘>‘) p[i]=2;
        if (c==‘=‘) p[i]=0;
    }
    for (int i=0;i<n;++i)
    {
        for (int j=0;j<n;++j)
        {
            a[j]=0;
            f[j]=j;
        }
        err=0;
        for (int j=1;j<=m && !err;++j)
        {
            xx=x[j];
            yy=y[j];
            if (xx==i || yy==i) continue;
            gfx=gf(xx);
            gfy=gf(yy);
            type=(a[xx]-a[yy]+p[j]+3)%3;
            if (gfy!=gfx)
            {
                f[gfy]=gfx;
                a[gfy]=type;
            }
            else if ((a[xx]+p[j])%3!=a[yy])
                err=j;
        }
        if (err)
        {
            cnt++;
            step=max(step,err);
        }
        else
            ans=i;
    }
    if (cnt==n) printf("Impossible\n");
    else if (cnt<n-1) printf("Can not determine\n");
    else printf("Player %d can be determined to be the judge after %d lines\n",ans,step);
}
int main()
{
    while (scanf("%d%d",&n,&m)==2)
    {
        solve();
    }
    return 0;
}
   

poj2912 Rochambeau

时间: 2024-08-30 12:12:35

poj2912 Rochambeau的相关文章

POJ2912 Rochambeau (hard?or!hard?)

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 childr

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

并查集练习2(带权并查集)

明天旅游去爬山逛庙玩,今天练一天然后早早睡觉啦~ poj1703 Find them, Catch them (带权并查集) 1 #include<cstdio> 2 const int N=1e5+1; 3 int f[N]; 4 int r[N];//表示与父节点的关系,0同类,1不同类 5 int n; 6 void init(){ 7 for(int i=1;i<=n;++i){ 8 f[i]=i; r[i]=0; 9 } 10 } 11 int fin(int x){ 12 i

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

图论五百题!

生死看淡不服就淦,这才是人生! =============================以下是最小生成树+并查集======================================[HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基础并查集★1325&&poj1308 Is It A Tree? 基础并查集★1856 More is better 基础并查集★1102 Constructing Roads 基础最小生成树★1232 畅通工程 基

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

并查集&amp;MST

[HDU] 1198 Farm Irrigation 基础最小生成树★ 1598 find the most comfortable road 枚举+最小生成树★★ 1811 Rank of Tetris 并查集+拓扑排序★★ 3926 Hand in Hand 同构图★ 3938 Portal 离线+并查集★★ 2489     Minimal Ratio Tree dfs枚举组合情况+最小生成树★ 4081     Qin Shi Huang's National Road System 最

图论精炼500题

忘了从哪转的了... =============================以下是最小生成树+并查集====================================== [HDU] 1213               How Many Tables                    基础并查集★ 1272               小希的迷宫                     基础并查集★ 1325&&poj1308    Is It A Tree?       

hdu图论题目分类

=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 基础并查集★ 1325&&poj1308 Is It A Tree? 基础并查集★ 1856 More is better 基础并查集★ 1102 Constructing Roads 基础最小生成树★ 1232 畅通工程 基础并查集★ 123