• POJ 2912


    题目链接: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

    题意:

    有n个人玩剪刀石头布,编号为0~n-1,;

    其中有且仅有一个人作为裁判,而其余人分成3组(组可以为空);

    这三个组分别只能出剪刀,石头和布,而裁判可以任意出;

    给出m次编号为a和编号为b的猜拳结果为c,要求输出最早在知道第几次猜拳结果后可以知道裁判是哪个人。

    题解:

    并查集建树;

    由于裁判可以任意出,所以包含裁判的边都是不可靠的,所以不能在建立关系时不能纳入裁判。

    所以做法是,枚举假设裁判为第jdg个人,每次假设都进行一次并查集建树操作;

    假设当前裁判编号为jdg,那么枚举m次猜拳结果时,对有他参与的直接跳过;

    那么,在并查集建树过程中,会出两种情况:出现矛盾,那么这个人不是裁判;没出现矛盾,这个人是裁判(当然这是建立在有且仅有一个裁判的基础上的)。

    (如果出现枚举n个人假设作为裁判,发现全部都有矛盾呢,显然这代表裁判不存在了,根据题意是要输出“Impossible”;

    如果出现两个及以上的人假设作为裁判,发现都没有矛盾,那么就不能判断谁是裁判了,输出“Can not determine”;)

    then,剩下的问题是,怎么知道是在知晓第几次猜拳结果后能断定谁是裁判了呢?

    由于此时,枚举过程中,除去一个人(就是裁判)不会产生矛盾,剩下的n-1个人都会产生矛盾;

    那么对于假设第jdg个人是裁判(但其实他并不是裁判),那么我们记录下:枚举m次猜拳结果,第一个矛盾产生的位置,记录为contradiction[jdg](也就是说在contradiction[jdg]这次之后,就知道jdg不是裁判了);

    显然的,我们至少需要知晓max(contradiction[i])次猜拳结果之后,才能判断除了裁判之外的所有人都不是裁判。

    AC代码:

    #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]代表x与par[x]比:0-平局,1-输了,2-赢了。
    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跟b比,平局
                    if(str[i]=='<') c=1; //a跟b比,赢了
                    if(str[i]=='>') c=2; //a跟b比,输了
                    break;
                }
                a*=10;
                a+=(str[i]-'0');
            }
            for(i++;str[i];i++)
            {
                b*=10;
                b+=(str[i]-'0');
            }
        }
    }in[maxm];
    
    int ctrdt[maxn]; //假设裁判为第i个人时,在第ctrdt[i]次猜拳后产生矛盾
    
    int main()
    {
        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++) //枚举假设裁判为第j个人
            {
                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)
                {
                    ans=jdg;
                    cnt++;
                }
                else line=max(line,ctrdt[jdg]);
            }
    
            if(cnt==1) printf("Player %d can be determined to be the judge after %d lines
    ",ans,line);
            else if(cnt==0) printf("Impossible
    ");
            else printf("Can not determine
    ");
        }
    }

    注意:并查集路径压缩时,val[]值的变动依然遵循HDU3038这篇文章中提到的向量加减规则,只是需要注意控制MOD=3,也即val[]在[0,2]内变动即可。

  • 相关阅读:
    使用UOS微信桌面版协议登录,wechaty免费版web协议又可以用了
    angular之$watch方法详解
    webpack配置这一篇就够
    select设置disable后ie修改默认字体颜色暂时解决
    201901251946
    new year
    test
    mysql密码忘记解决方法
    bianmayujianmatest
    jinzhizhuanhuan
  • 原文地址:https://www.cnblogs.com/dilthey/p/8964853.html
Copyright © 2020-2023  润新知