zoukankan      html  css  js  c++  java
  • POJ

    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

    分析:
    题目跟许多人的相对关系有关,可以想到带权并查集。但是裁判的手势是不一定的,直接加入集合的话,会让关系变得很乱。
    题目中的n很小,所以我们可以枚举每个人是裁判的情况,将除裁判以外的人加入到集合中,看是否存在矛盾。
    如果这种情况不唯一,则不能决定谁是裁判,如果无论谁是裁判都存在矛盾,则代表该情况下不可能。
    如果找到裁判,那么枚举其他人的时候,出现矛盾的最大的回合数就是要找的回合数。
    代码如下:
    #include<cstdio>
    #include <cmath>
    #include <queue>
    #include <stdlib.h>
    #include<iostream>
    #include<cstring>
    #include<vector>
    using namespace std;
    const int MAXN=500+10;
    int F[MAXN];
    int val[MAXN];
    int vis[MAXN];
    char str[10];
    int n,m,a,b,num,flag2;
    void init()
    {
         for(int i=0;i<n;i++)
          {
              F[i]=-1;
              val[i]=0;
          }
    }
    struct node
    {
        int a;
        int ch;
        int b;
    }op[2010];
    int Find(int x)
    {
        if(F[x]==-1||F[x]==x)return x;
      int tmp=Find(F[x]);
        val[x]+=val[F[x]];
        val[x]%=3;
        return F[x]=tmp;
    }
    int flag1,ansi,ansj,cnt;
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            flag2=0;
            cnt=0;
            ansj=-1;
            if(n==1)
            {
                puts("Player 0 can be determined to be the judge after 0 lines");
                continue;
            }
           for(int k=1;k<=m;k++){
               scanf("%s",str);
               int len=strlen(str);
               a=0;
               int tel;
               for(int i=0;i<len;i++)
               {
                   if(str[i]>='0'&&str[i]<='9')
                      a=a*10+str[i]-'0';
                   else
                   {
                       tel=i;
                       break;
                   }
               }
               b=0;
               for(int i=tel+1;i<len;i++)
                  b=b*10+str[i]-'0';
                 op[k].a=a;
                 op[k].b=b;
                 if(str[tel]=='=')op[k].ch=0;
                 else if(str[tel]=='>')op[k].ch=1;
                 else if(str[tel]=='<')op[k].ch=2;
              }
          for(int i=0;i<n;i++)
          {
              init();
              flag1=1;
              for(int j=1;j<=m;j++)
              {
                  a=op[j].a;
                  b=op[j].b;
                  if(a==i||b==i)continue;
                 int t1=Find(a);
                 int t2=Find(b);
                 if(t1!=t2)
                 {
                    F[t1]=t2;
                    val[t1]=(val[b]-val[a]+op[j].ch+3)%3;
                 }
                 else
                 {
                    if((val[a]-val[b]+3)%3!=op[j].ch)
                    {
                       ansj=max(ansj,j);
                      flag1=0;
                      break;
                    }
                 }
              }
              if(flag1==1)
              {
                cnt++;
                flag2=1;
                ansi=i;
              }
          }
          if(flag2==0)puts("Impossible");
          else if(cnt>=2)puts("Can not determine");
          else printf("Player %d can be determined to be the judge after %d lines
    ",ansi,ansj);
        }
        return 0;
    }


  • 相关阅读:
    ECMAScript6 入门 函数的扩展
    ECMAScript6 入门-let与const命令
    编码规范
    webpack常用插件
    JS与CSS那些特别小的知识点区别
    常见数组方法及细节
    JS库
    Object冷知识
    html5-语义化标签
    Css继承属性和非继承属性
  • 原文地址:https://www.cnblogs.com/a249189046/p/8183863.html
Copyright © 2011-2022 走看看