POJ 2912 Rochambeau(枚举+并查集)

Rochambeau

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 1575 Accepted: 540

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 Mrounds. 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/        

枚举裁判,然后并查集判断

裁判由于可以任意出,所以可能属于任意一个集合,所以有裁判参与的会合不考虑,然后并查集部分和食物链很相似。

如果某个裁判那里出现了矛盾,则记录一下在哪出问题。

然后判断是否只有一个裁判没有出现问题。如果只有一个,说明可以确定,那么就是剩下的人出问题的最大值。因为只有否定了其它所有人,才能确定

 

 

/*
POJ 2912
枚举+并查集
枚举每一个裁判,看有没有不出错的
如果没有,说明是Impossible
如果有超过一个,那么就是Can not determine
如果只有一个,那么输出其他出错的位置的最大值
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN=510;
const int MAXM=2010;
struct Node
{
    int u,v;
    int re;
}node[MAXM];
int F[MAXN];
int val[MAXN];
int find(int x)
{
    if(F[x]==-1)return x;
    int tmp=find(F[x]);
    val[x]+=val[F[x]];
    val[x]%=3;
    return F[x]=tmp;
}
char str[30];
int main()
{
    int n,m;
    int u,v;
    while(scanf("%d%d",&n,&m)==2)
    {
        gets(str);
        for(int i=0;i<m;i++)
        {
            //scanf("%s",&str);
            gets(str);
            int t=0;
            int len=strlen(str);
            for(t=0;t<len;t++)
              if(str[t]=='>'||str[t]=='='||str[t]=='<')
                break;
            u=0;
            for(int j=0;j<t;j++)
            {
                u*=10;
                u+=str[j]-'0';
            }
            v=0;
            for(int j=t+1;j<len;j++)
            {
                v*=10;
                v+=str[j]-'0';
            }
            node[i].u=u;
            node[i].v=v;
            if(str[t]=='=')node[i].re=0;
            else if(str[t]=='<')node[i].re=1;
            else node[i].re=2;
        }
        int ansi;
        int anst=0;
        int t0=0;//不矛盾的个数
        for(int i=0;i<n;i++)
        {
            memset(F,-1,sizeof(F));
            memset(val,0,sizeof(val));
            int ff=-1;
            for(int j=0;j<m;j++)
            {
                if(node[j].u==i || node[j].v==i)continue;
                u=node[j].u;
                v=node[j].v;
                int t1=find(u);
                int t2=find(v);
                if(t1==t2)
                {
                    if(val[v]!=(val[u]+node[j].re)%3)
                    {
                        ff=j+1;
                        break;
                    }
                }
                else
                {
                    F[t2]=t1;
                    val[t2]=val[u]-val[v]+node[j].re;
                    val[t2]=(val[t2]+3)%3;
                }
            }
            if(ff==-1)
            {
                ansi=i;
                t0++;
            }
            else anst=max(anst,ff);
        }
        if(t0==0)printf("Impossible\n");
        else if(t0>=2)printf("Can not determine\n");
        else
           printf("Player %d can be determined to be the judge after %d lines\n",ansi,anst);
    }
    return 0;
}

 

    原文作者:kuangbin
    原文地址: https://www.cnblogs.com/kuangbin/archive/2013/04/05/3001668.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞