poj 2912 并查集

Rochambeau
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 1658   Accepted: 577

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

这道题和poj上的1182食物链有着异曲同工之妙,题意是N个小孩正在和你玩一种剪刀石头布游戏。N个小孩中有一个是裁判,其余小孩分成三组(不排除某些组没有任何成员的可能性),但是你不知道谁是裁判,也不知道小孩们的分组情况。然后,小孩们开始玩剪刀石头布游戏,一共玩M次,每次任意选择两个小孩进行一轮,你会被告知结果,即两个小孩的胜负情况,然而你不会得知小孩具体出的是剪刀、石头还是布。已知各组的小孩分别只会出一种手势(因而同一组的两个小孩总会是和局),而裁判则每次都会随便选择出一种手势,因此没有人会知道裁判到底会出什么。请你在M次剪刀石头布游戏结束后,猜猜谁是裁判。如果你能猜出谁是裁判,请说明最早在第几次游戏结束后你就能够确定谁是裁判。
这道题的思路就是:
石头-->箭头-->布-->石头...食物链问题.


dis[i]=0:i 和 root 同组.


dis[i]=1:i 吃 root.


dis[i]=2:i 被 root 吃.


判断谁是裁判,用枚举即可.


如果枚举的是裁判的话,那么除了裁判参与的局,其余局势肯定是正常的.


之后就是逻辑分析了,看看什么情况"Impossible",什么情况"Can not determine"了.
下面是代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 600
#define M 2000
using namespace std;
int pre[N],rank[N],n,a[M],b[M];
char op[M];
int max(int a,int b) {return a>b?a:b;}

int find(int x){
    if(x!=pre[x]){
          int fx=find(pre[x]);
          rank[x]=(rank[x]+rank[pre[x]])%3;
          pre[x]=fx;
    }
    return pre[x];
}

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


你可能感兴趣的:(poj 2912 并查集)