1. 程式人生 > >【POJ2912】Rochambeau(帶權並查集+列舉)

【POJ2912】Rochambeau(帶權並查集+列舉)

題目連結

Rochambeau

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions:4932 Accepted: 1699

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個小朋友玩剪刀石頭布,分成3組,其中有一個裁判,同一組的小朋友每次出相同的手勢,只有裁判每次出的手勢是隨機的,從進行m輪遊戲,已知每輪遊戲後的結果,輸出裁判的序號,並且輸出至少經過幾輪遊戲才能判斷出該人是裁判,如果沒有裁判則輸出Impossible,如果裁判數量過多不能確定則輸出Can not determine。

【解題思路】

將每局比賽的結果量化為0,1,2,用並查集去維護,列舉0-n-1個人分別是裁判的情況,並且除去和裁判遊戲的輪數,如果node[j].op!=(sum[node[j].a]-sum[node[j].b]+3)%3則說明這個人不是裁判,標記一下,最後找出為沒被標記的序號則是裁判。

【程式碼】

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=505;
int pre[maxn],sum[maxn],f[maxn],index[maxn];
int n,m;
struct Node
{
    int op,a,b;
}node[2005];
int findroot(int x)
{
    if(x!=pre[x])
    {
        int t=pre[x];
        pre[x]=findroot(pre[x]);
        sum[x]=(sum[x]+sum[t]+3)%3;
    }
    return pre[x];
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int a,b,ans=0,ansindex=0,cnt=0;
        char ch;
        memset(f,1,sizeof(f));
        memset(index,0,sizeof(index));
        for(int i=0;i<m;i++)
        {
            scanf("%d%c%d",&a,&ch,&b);
            if(ch=='=')node[i].op=0;
            else if(ch=='>')node[i].op=1;
            else node[i].op=2;
            node[i].a=a;node[i].b=b;
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)pre[j]=j;
            memset(sum,0,sizeof(sum));
            for(int j=0;j<m;j++)
            {
                if(node[j].a==i || node[j].b==i)continue;
                int fa=findroot(node[j].a);
                int fb=findroot(node[j].b);
                if(fa!=fb)
                {
                    pre[fa]=fb;
                    sum[fa]=(sum[node[j].b]-sum[node[j].a]+node[j].op+3)%3;
                }
                else
                {
                    if(node[j].op!=(sum[node[j].a]-sum[node[j].b]+3)%3)
                    {
                        f[i]=0;
                        index[i]=j+1;
                        break;
                    }
                }
            }
        }
        for(int i=0;i<n;i++)
        {
            if(f[i])
            {
                cnt++;
                ans=i;
            }
            else
                ansindex=max(ansindex,index[i]);
        }
        if(cnt==1)printf("Player %d can be determined to be the judge after %d lines\n",ans,ansindex);
        else if(cnt>1)printf("Can not determine\n");
        else if(cnt==0)printf("Impossible\n");
    }
    return 0;
}