1. 程式人生 > >POJ2912:Rochambeau(帶權並查集)

POJ2912:Rochambeau(帶權並查集)

Rochambeau

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5208   Accepted: 1778

題目連結http://poj.org/problem?id=2912

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個小朋友被分為三組玩剪刀石頭布,每一組的小朋友只出固定的招數,但是這些小朋友裡面有個裁判,他可以任意出,現在問是否能夠確定出這個裁判,最少經過幾輪?

 

題解:

三個組並且是個關於輸贏的環狀關係,所以我們可以思考帶權並查集,v[x]代表x與其父親結點的關係,v[x]=0代表同類,v[x]=1代表x贏他父親,v[x]=2就代表輸了。

現在關鍵是這個裁判,我們通過思考可以發現,假如確定了這個人為裁判,那麼他參與的幾次都不算做分組。

但如何去確定就是個問題。注意這題時間限制挺大的,所以我們直接列舉,假定每個人都作為裁判就行了。

最後還要確定經過幾輪遊戲,這裡挺有意思的:

確定目前這個人為裁判,這等價於排除其他所有人不是裁判的最少次數,這個在列舉其他人作為裁判時就可以進行處理了。

 

程式碼如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

const int N = 2505;
int n,m;
int f[N],a[N],b[N],v[N],ans[N];
char c[N];

int find(int x){
    if(x==f[x]) return x;
    int tmp=f[x];
    f[x]=find(f[x]);
    v[x]=(v[x]+v[tmp])%3;
    return f[x];
}

int main(){
    while(~scanf("%d%d",&n,&m)){
        for(int i=1;i<=m;i++) scanf("%d%c%d",&a[i],&c[i],&b[i]);
        int tot = 0,cnt = 0,l = 0;
        memset(ans,0,sizeof(ans));
        for(int i=0;i<n;i++){
            bool flag=true;cnt=0;
            for(int j=0;j<=n;j++) f[j]=j,v[j]=0;
            for(int j=1;j<=m;j++){
                if(a[j]==i || b[j]==i) continue;
                int fx=find(a[j]),fy=find(b[j]);
                if(c[j]=='>'){
                    if(fx==fy && (v[a[j]]+3-v[b[j]])%3!=2) flag=false,cnt=j;
                    else{
                        f[fx]=fy;
                        v[fx]=(3-v[a[j]]+2+v[b[j]])%3;
                    }
                }else if(c[j]=='<'){
                    if(fx==fy && (v[a[j]]+3-v[b[j]])%3!=1) flag=false,cnt=j;
                    else{
                        f[fx]=fy;
                        v[fx]=(3-v[a[j]]+1+v[b[j]])%3;
                    }                    
                }else{
                    if(fx==fy && (v[a[j]]+3-v[b[j]])%3!=0) flag=false,cnt=j;
                    else{
                        f[fx]=fy;
                        v[fx]=(3-v[a[j]]+v[b[j]])%3;
                    }
                }
                if(!flag) break;
            }
            if(flag) ans[++tot]=i;
            l=max(l,cnt);
        }
        if(tot==0) puts("Impossible");
        else if(tot>1) puts("Can not determine");
        else printf("Player %d can be determined to be the judge after %d lines\n",ans[1],l);
    }
    return 0;
}