K - Rochambeau POJ - 2912

K - Rochambeau

POJ - 2912

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

AC代码

//#include<bits/stdc++.h>
#define _CRT_SBCURE_NO_DEPRECATE
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
//#define UP(i,x,y) for(int i=x;i<=y;i++)
//#define DOWN(i,x,y) for(int i=x;i>=y;i--)
//#define sd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
using namespace std;
const int maxn = 200500;
const int INF = 0x3f3f3f3f;

int n, m;
int ans;int len;
int par[maxn];
int val[maxn];
int cnt;
struct node
{
    int x, y;
    char c;
    node(int x,int y, char c):x(x),y(y),c(c){}
};
vector<node> que;
void init()
{
    for(int i = 0; i <= n; i++){
        par[i] = i;
        val[i] = 0;
    }
}
int Find(int x)
{
    if(par[x] == x) return x;
    else{
        int fa = par[x];
        par[x] = Find(par[x]);
        val[x] = (val[fa] + val[x]) % 3;
        return par[x];
    }
}
void unite(int x, int y, int flag)
{
    int fx = Find(x);
    int fy = Find(y);
    if(fx == fy) return;
    else{
        par[fx] = fy;
        if(flag == 0){
            if(val[x] == val[y])
                val[fx] = 0;
            else if((val[x]==0&&val[y]==1)||(val[x]==1&&val[y]==2)||(val[x]==2&&val[y]==0))
                val[fx] = 1;
            else
                val[fx] = 2;
        }else if(flag == 1){
            if(val[x] == val[y])
                val[fx] = 1;
            else if((val[x]==0&&val[y]==1)||(val[x]==1&&val[y]==2)||(val[x]==2&&val[y]==0))
                val[fx] = 2;
            else
                val[fx] = 0;
        }else if(flag == 2){
            if(val[x] == val[y])
                val[fx] = 2;
            else if((val[x]==0&&val[y]==1)||(val[x]==1&&val[y]==2)||(val[x]==2&&val[y]==0))
                val[fx] = 0;
            else
                val[fx] = 1;
        }
    }
}
int main()
{
	//freopen("in.txt", "r", stdin);
	while(~scanf("%d%d", &n, &m))
    {
        if(n == 1)
        {
             printf("Player 0 can be determined to be the judge after 0 lines\n");
             continue;
        }
        getchar();
        que.clear();
        int ta, tb; char tc;
        for(int i = 1; i <= m; i++)
        {
            scanf("%d%c%d", &ta, &tc, &tb);getchar();
            que.pb(node(ta,tb,tc));
        }
        ans = 0;
        cnt = 0;
        len = 0;
        for(int i = 0; i < n; i++)
        {
            init();
            int ff = -1;
            for(int j = 0; j < que.size(); j++)
            {
                node nn = que[j];
                ta = nn.x;tb = nn.y;tc = nn.c;
                if(ta == i || tb == i) continue;
                if(tc=='=')
                {
                    int fx = Find(ta);int fy = Find(tb);
                    if(fx == fy)
                    {
                        if(val[ta] == val[tb])
                            continue;
                        else
                        {
                            ff = j+1;
                            break;
                        }
                    }
                    else
                    {
                        unite(ta, tb, 0);
                    }
                }
                if(tc=='<')
                {
                    int fx = Find(ta);int fy = Find(tb);
                    if(fx == fy)
                    {
                        if((val[ta]==0&&val[tb]==2)||(val[ta]==1&&val[tb]==0)||(val[ta]==2&&val[tb]==1))
                            continue;
                        else
                        {
                            ff = j+1;
                            break;
                        }
                    }
                    else
                    {
                        unite(ta, tb, 1);
                    }
                }
                if(tc=='>')
                {
                    int fx = Find(ta);int fy = Find(tb);
                    if(fx == fy)
                    {
                        if((val[ta]==0&&val[tb]==1)||(val[ta]==1&&val[tb]==2)||(val[ta]==2&&val[tb]==0))
                            continue;
                        else

                        {
                            ff = j+1;
                            break;
                        }
                    }
                    else
                    {
                        unite(ta, tb, 2);
                    }
                }
            }
            if(ff==-1)
            {
                ans=i;
                cnt++;
            }
            else len=max(len,ff);
        }
        if(!cnt)
        {
            printf("Impossible\n");
        }
        else if(cnt >= 2)
        {
            printf("Can not determine\n");
        }
        else
        {
            printf("Player %d can be determined to be the judge after %d lines\n", ans, len);
        }
    }


	return 0;
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值