POJ 2912 Rochambeau(枚举 + 偏移量并查集)

题意:这个题的题意,我一开始没太弄懂裁判的功能是什么,我还以为裁判不参与比赛。 后来才明白裁判是个作弊人,别的小朋友每次能出一种拳, 裁判想出什么出什么,让你判断谁是裁判。

解题思路:问题的关键是,一开始没裁判的情况下都是有序的,不存在矛盾的。有了裁判,因为裁判能乱出拳, 就可能会造成矛盾(只是可能),所以如果一组内去掉某一个player后不存在矛盾了,就说明这个player可能会是裁判(不是说明他就是裁判了),如果有是裁判这种可能的player只有一个,那么他就一定是裁判,如果是裁判可能的player有多个,那就搞不清楚谁到底是裁判了,输出指定信息。再如果一个可能是裁判的人都没有, 与题意矛盾, 输出错误信息即可。最后一个矛盾的信息,就是最终确定裁判的信息。


问题描述

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


输出

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.


样例输入

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

样例输出

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


Memory: 708 KB Time: 766 MS
Language: G++ Result: Accepted

#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cctype>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>

using namespace std;

/**************************************************************/
#define MAX_N 505
const int INF = 0x3f3f3f3f;
int parent[MAX_N * 3];

struct node
{
    int a, b;
    char s;
} t[MAX_N];

inline void init(int n)
{
    for(int i = 0 ; i < n * 3 + 1 ; i++)
    {
        parent[i] = i;
    }
}

int find(int a)
{
    if(parent[a] != a)
    {
        parent[a] = find(parent[a]);
    }
    return parent[a];
}

void unite(int a, int b)
{
    int fa = find(a);
    int fb = find(b);
    if(fa != fb)
    {
        parent[fb] = fa;
    }
    return;
}

bool same(int a, int b)
{
    return find(a) == find(b);
}

int main()
{
    int n, m;
    while(~scanf("%d%d", &n , &m))
    {
        init(n);
        for(int i = 0 ; i < m ; i++)
        {
            scanf("%d%c%d", &t[i].a, &t[i].s, &t[i].b);
//            cout<<t[i].a<<t[i].s<<t[i].b<<endl;
        }
        int cnt = 0;
        int num = 0;

        int player = 0;
        for(int k = 0 ; k < n ; k++)
        {
            int flag = 0;
            init(n);
            for(int i = 0 ; i < m ; i++)
            {
                if(t[i].a == k || t[i].b == k) continue;
                int a = t[i].a;
                int b = t[i].b;
                if(t[i].s == '=')
                {
                    if(same(a, b + n) || same(a, b + 2 * n))
                    {
                        flag = 1;
//                        cout<<1<<endl;
                        num = max(num, i);
                        break;
                    }
                    unite(a, b);
                    unite(a + n , b + n);
                    unite(a + 2 * n, b + 2 * n);
                }
                else if(t[i].s == '<')//
                {
                    if(same(a, b) || same(a, b + 2 * n))
                    {
                        flag = 1;
//                        cout<<2<<endl;
                        num = max(num, i);
                        break;
                    }
                    unite(a, b + n);
                    unite(a + n, b + 2 * n);
                    unite(a + 2 * n, b);
                }
                else
                {
                    if(same(a, b) || same(a, b + n))
                    {
                        flag = 1;
//                        cout<<3<<endl;
//                        cout<<"k"<<" "<<k<<endl;
                        num = max(num, i);
                        break;
                    }
                    unite(a, b + 2 * n);
                    unite(a + n, b);
                    unite(a + 2 * n, b + n);
                }
            }
            if(!flag)
            {
                cnt++;
                player = k;
            }
        }
        if(cnt == 1)
            printf("Player %d can be determined to be the judge after %d lines\n", player, num + 1);
        else if(cnt > 1)
            printf("Can not determine\n");
        else
            printf("Impossible\n");
//        printf("%d\n", cnt);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值