POJ 2912 - Rochambeau - [Violent enumeration + weighted union]

Problem link: http://poj.org/problem?id=2912

Time Limit: 5000MS Memory Limit: 65536K

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

 

Title:

There are n people playing rock-paper-scissors, numbered 0~n-1,;

There is only one person among them as the referee, and the rest are divided into 3 groups (the group can be empty);

These three groups can only play scissors, rock and cloth respectively, and the referee can play them arbitrarily;

Given m times of box-guessing results numbered a and number b, the result is c, and the output is required to know who the referee is after knowing the number of box-guessing results at the earliest.

 

answer:

And check and build trees;

Since the referee can be arbitrarily chosen, the sides that contain the referee are unreliable, so the referee cannot be included when establishing a relationship.

Therefore, the method is to enumerate the assumption that the referee is the jdg person, and perform a concatenation and tree-building operation for each assumption;

Assuming that the current referee number is jdg, then when enumerating the results of m times of boxing, skip directly to those who have participated in it;

Then, in the process of building a union, search and set, there will be two situations: if there is a contradiction, then this person is not a referee; if there is no contradiction, this person is a referee (of course, this is based on the fact that there is one and only one referee) .

(If there is an enumeration of n hypotheses as judges, and all of them are found to be contradictory, obviously this means that the judge does not exist, and according to the meaning of the question, it is necessary to output "Impossible";

If there are two or more people who are supposed to be judges, and there is no contradiction, then it is impossible to judge who is the judge, and the output "Can not determine" ;)

 

Then, the remaining question is, how do you know who is the referee after knowing the result of the fist guess?

Because at this time, in the enumeration process, there will be no contradictions except for one person (that is, the referee), and the remaining n-1 people will have contradictions;

Then, for the assumption that the jdg person is the referee (but he is not actually a referee), then we record: enumerate the results of m fist guessing, and record the position of the first contradiction as contrast[jdg] (that is to say, in contrast[jdg] jdg] After this time, I know that jdg is not a referee);

Obviously, we need to know at least the results of max(contradiction[i]) times of guessing before we can judge that everyone except the referee is not a referee.

 

AC code:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=500+10;
const int maxm=2000+10;

int n,m;

int par[maxn],val[maxn]; // val[x] represents the ratio of x to par[x]: 0 - draw, 1 - lose, 2 - win. 
void init( int l, int r){ for ( int i=l;i<=r;i++) par[i]=i,val[i]= 0 ;}
 int find( int x)
{
    if(par[x]==x) return x;
    else
    {
        int root=find(par[x]);
        val[x] =(val[x]+val[par[x]])% 3 ;
        return par[x]= root;
    }
}

struct IN{
    int a,b,c;
    void get(char str[])
    {
        a=b=0;
        int i;
        for(i=0;str[i];i++)
        {
            if(str[i]=='='||str[i]=='<'||str[i]=='>')
            {
                if (str[i]== ' = ' ) c= 0 ; // a vs b, draw 
                if (str[i]== ' < ' ) c= 1 ; // a vs b, win 
                if (str[i]== ' > ' ) c= 2 ; // a compares with b and loses 
                break ;
            }
            a*=10;
            a+=(str[i]-'0');
        }
        for(i++;str[i];i++)
        {
            b*=10;
            b+=(str[i]-'0');
        }
    }
} in [maxm];

int ctrdt[maxn]; // Assuming that the referee is the ith person, a contradiction occurs after the ctrdt[i]th tick guess

intmain ()
{
    while(scanf("%d%d%",&n,&m)!=EOF)
    {
        for(int i=1;i<=m;i++)
        {
            char tmp[10];
            scanf("%s",tmp);
            in[i].get(tmp);
        }

        memset(ctrdt, 0 , sizeof (ctrdt));
         for ( int jdg= 0 ;jdg<n;jdg++) // enumeration assumes the referee is the jth person 
        {
            init(0,n);
            for(int i=1;i<=m;i++)
            {
                int a=in[i].a, b=in[i].b, c=in[i].c;
                if(a==jdg || b==jdg) continue;

                int t1=find(a),t2=find(b);
                if(t1!=t2)
                {
                    par[t2] = t1;
                    val[t2]=((val[a]-c+3)%3-val[b]+3)%3;
                }
                else
                {
                    if( ((val[a]-c+3)%3-val[b]+3)%3 != 0 )
                    {
                        ctrdt[jdg]=i;
                        break;
                    }
                }
            }
        }

        int cnt=0,ans;
        int line=0;
        for(int jdg=0;jdg<n;jdg++)
        {
            if(ctrdt[jdg]==0)
            {
                years = jdg;
                cnt++;
            }
            else line=max(line,ctrdt[jdg]);
        }

        if(cnt==1) printf("Player %d can be determined to be the judge after %d lines\n",ans,line);
        else if(cnt==0) printf("Impossible\n");
        else printf("Can not determine\n");
    }
}

Note: When compressing the union search path, the change of the val[] value still follows the vector addition and subtraction rules mentioned in this article HDU3038 , but it is necessary to pay attention to control MOD=3, that is, val[] is within [0,2] You can change it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324986728&siteId=291194637