POJ 2912 Rochambeau

Source: Internet
Author: User
Tags bool rounds

Link:

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

Topic:

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 this some group is empty). You are don ' t know who's 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. For one Rochambeau, and you'll be once the ' told while N OT knowing which gesture the children presented. It is known this children in the same group would present the same gesture (hence, two children in the same group Alwa Ys get draw to playing) and different groups for different gestures. The judge would present gesture randomly each time, hence no one knows what the gesture the judge would. Can you guess who's the judge after the game ends? If can, after how many rounds can your 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 is "=", ">" or "<", referring to a draw, which is the "a" and "the" "second child wins Respectivel Y.

Output

There is only one line for each test case. If The judge can is 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 is 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 is determined to is the judge after 4 lines
impossible
player 0 can be deter Mined to is the judge after 0 lines

Source

Baidu Star 2006 Preliminary

The main effect of the topic:

N Personal play, playing stone scissors cloth game, of which 1 people are referees, the rest of the n-1 individual divided into 3 groups, they discussed, the same group of people each time out the same gesture, different groups of people are different, and the referee is random out. Give m a result, judge that is the referee.

Analysis and Summary:

The problem was tangled yesterday night without AC, and today I had another night.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

The key is to enumerate each person in turn, which is hard to imagine, the person who has enumerated the referee, and then the other does not include the referee of those who judge, the method of judgment and the food chain that question. If there is no contradiction, it means that this person is the referee.

Finally, see how many people may be referees, if 0, description is impossible, 1 instructions to find the only referee, more than 1 instructions is can not determine.

If so, how do you determine which step to start? The answer is that, in addition to other enumerations, the one with the largest number of contradictory steps is the number of steps that can be determined by the referee.

Code:

#include <cstdio> #include <cmath> #include <cstring> using namespace std;  
const int N = 510, M = 2005;  
int n,m,f[n],rank[n],cnt, time[n];  
int a[m], b[m];  
    
Char C[m];  
inline void init () {for (int i=0; i<=n; ++i) f[i]=i, rank[i]=0;  
    int find (int x) {if (x==f[x]) return x;  
    int fa=f[x];  
    F[X] = find (f[x]);  
    RANK[X] = (RANK[X]+RANK[FA])%3;  
return f[x];  
    inline bool Union (int x,int y,int d) {int a=find (x), B=find (y);  
        if (a==b) {if (D==0&&rank[x]!=rank[y]) return false;  
        if (d==1) {if ((rank[x]+1)%3!=rank[y]) return false;  
    return true;  
    } F[b] = A;  
    RANK[B] = (rank[x]-rank[y]+3+d)%3;  
return true;  
    int main () {int a,b;  
    Char ch;  
            while (~SCANF ("%d%d", &n,&m)) {for (int i=0; i<m; ++i) {scanf ("%d", &a[i]);  
    scanf ("%c", &c[i]);        Prevent a space while (c[i]== ') scanf ("%c", &c[i]);  
        scanf ("%d", &b[i]);    
        int cnt=0, cur=0, person=0;  
        memset (time, 0, sizeof (time));  
            Enumerates each judge for (int i=0; i<n; ++i) {init ();  
            BOOL Flag=false; for (int j=0; j<m; ++j) if (a[j]!=i && b[j]!=i) {if (c[j]== ' = ' &&!)   
                    Union (a[j],b[j],0)) {flag=true;  
                    if (J>time[i]) {time[i]=j+1;}   
                Break } if (c[j]== ' < ' &&!)   
                    Union (a[j],b[j],1)) {flag=true;  
                    if (J>time[i]) {time[i]=j+1;}   
                Break } if (c[j]== ' > ' &&!)   
                    Union (b[j],a[j],1)) {flag=true;  
                    if (J>time[i]) {time[i]=j+1;}   
              Break  } if (!flag) {person = i;  
            ++cnt;  
        } if (cnt==0) puts ("impossible");  
            else if (cnt==1) {for (int i=0; i<n; ++i) if (i!=person&&time[i]>cur) cur=time[i];  
        printf ("Player%d can be determined to being" judge after%d lines\n ", person, cur);"  
    Else puts ("Can not Determine");  
return 0; }

Author: csdn Blog shuangde800

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.