POJ 2912 Rochambeau【并查集经典应用同食物链+枚举】

76 篇文章 0 订阅
21 篇文章 0 订阅

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 1603 Accepted: 556

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

Source

Baidu Star 2006 Preliminary 
Chen, Shixi (xreborner) living in http://fairyair.yeah.net/

一道明了了思路后 还是 WA了无数道的题目,最终完全转换成食物链那题了才彻底 AC掉抓狂

题意:有 N 个小孩玩游戏 ,编号 1 到 N-1 ,其中有一个是裁判,裁判可以出任意的手势,影响结果。

           最后让你判断谁是裁判。


算法:并查集经典应用


思路:枚举每一个点(<500)为裁判

          然后完完全全转换为食物链那题判断矛盾。。。

          加边时屏蔽掉有裁判的边。

           一旦发现矛盾 ,则说明此人不是裁判 ,并且更新最大的发生矛盾的边。

           最后如果只有一个裁判,则输出结果。

           如果有多个裁判,则输出无法判断。

           如果没有裁判,则输出不可能。。。

具体看代码Orz

OAccepted204 KB688 msC++2540 B2013-04-14 15:54:08

 #include<cstdio>
 #include<algorithm>
 using namespace std;

 const int maxn = 505;

 int p[maxn]; //父节点
 int r[maxn]; //与父节点的关系

 struct Node
 {
     int op;
     int x, y;
 }node[2005];

 int find(int x)
 {
     if(x == p[x]) return x;

     int t = p[x];
     p[x] = find(p[x]);
     r[x] = (r[x]+r[t])%3; //回溯更新与父节点的关系
     return p[x];
 }

void Union(int x, int y, int d) //转换为 x 吃 y或者同类
{
    int fx = find(x);
    int fy = find(y);

    p[fy] = fx; //被 x 吃, 以 x 的根为父
    r[fy] = (r[x]+d+(3-r[y]))%3; //d = 0 同类 ; d = 1 x吃y
}
 void set(int n)
 {
     for(int i = 0; i <= n; i++)
     {
         p[i] = i;
         r[i] = 0;
     }
 }
 int main()
 {
     int n, m;
     while(scanf("%d%d", &n, &m) != EOF)
     {
         char c;
         for(int i = 1; i <= m; i++)
         {
             scanf("%d%c%d", &node[i].x, &c, &node[i].y);

             if(c == '=') node[i].op = 0; //同类
             if(c == '<') node[i].op = 1; // x吃y
             else if(c == '>') node[i].op = 2; // y吃x 加边时注意转换一下
         }

         int judge;
         int Line = 0;
         int Wrong = 0;
         for(int i = 0; i < n; i++)
         {
             set(n);
             bool result = true;
             for(int j = 1; j <= m; j++)
             {
                 if(node[j].x == i || node[j].y == i) continue; //屏蔽裁判

                 int x = node[j].x;
                 int y = node[j].y;
                 int d = node[j].op;

                 //转换为同类或者 x 吃 y

                 if(node[j].op == 1) swap(x, y);
                 else if(node[j].op == 2) d = 1;

                 int fx = find(x);
                 int fy = find(y);

                 if(fx == fy)
                 {
                     if((r[x]+d)%3 != r[y])//矛盾 d == 0 同类 d == 1 x 吃 y
                     {
                         result = false;
                         Wrong++;
                         Line = max(Line, j); //边最大的判断矛盾数。。。
                         break;
                     }

                 }
                 else Union(x, y, d);
             }
             if(result) judge = i; //如果全都符合要求,则确定裁判
         }
         if(Wrong == (n-1)) printf("Player %d can be determined to be the judge after %d lines\n", judge, Line);
         else if(Wrong < (n-1)) printf("Can not determine\n");
         else printf("Impossible\n"); //Wrong == n没有一个裁判
     }
     return 0;
 }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值