题目链接: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=+;
const int maxm=+; 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]=;}
int find(int x)
{
if(par[x]==x) return x;
else
{
int root=find(par[x]);
val[x]=(val[x]+val[par[x]])%;
return par[x]=root;
}
} struct IN{
int a,b,c;
void get(char str[])
{
a=b=;
int i;
for(i=;str[i];i++)
{
if(str[i]=='='||str[i]=='<'||str[i]=='>')
{
if(str[i]=='=') c=; //a跟b比,平局
if(str[i]=='<') c=; //a跟b比,赢了
if(str[i]=='>') c=; //a跟b比,输了
break;
}
a*=;
a+=(str[i]-'');
}
for(i++;str[i];i++)
{
b*=;
b+=(str[i]-'');
}
}
}in[maxm]; int ctrdt[maxn]; //假设裁判为第i个人时,在第ctrdt[i]次猜拳后产生矛盾 int main()
{
while(scanf("%d%d%",&n,&m)!=EOF)
{
for(int i=;i<=m;i++)
{
char tmp[];
scanf("%s",tmp);
in[i].get(tmp);
} memset(ctrdt,,sizeof(ctrdt));
for(int jdg=;jdg<n;jdg++) //枚举假设裁判为第j个人
{
init(,n);
for(int i=;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+)%-val[b]+)%;
}
else
{
if( ((val[a]-c+)%-val[b]+)% != )
{
ctrdt[jdg]=i;
break;
}
}
}
} int cnt=,ans;
int line=;
for(int jdg=;jdg<n;jdg++)
{
if(ctrdt[jdg]==)
{
ans=jdg;
cnt++;
}
else line=max(line,ctrdt[jdg]);
} if(cnt==) printf("Player %d can be determined to be the judge after %d lines\n",ans,line);
else if(cnt==) printf("Impossible\n");
else printf("Can not determine\n");
}
}

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

POJ 2912 - Rochambeau - [暴力枚举+带权并查集]的更多相关文章

  1. poj 2912 Rochambeau(枚举+带权并查集)

    题目链接:http://poj.org/problem?id=2912 题意:多个人玩石头剪刀布分成3组和一个裁判,每一组提前选定了自己出哪个手势,裁判可以随意出什么手势,问是否能够从给出的一系列石头 ...

  2. POJ 2912 Rochambeau(暴力)+【带权并查集】

    <题目链接> 题目大意: n个人进行m轮剪刀石头布游戏(0<n<=500,0<=m<=2000),接下来m行形如x, y, ch的输入,ch='='表示x, y平局 ...

  3. POJ 2912 Rochambeau(难,好题,枚举+带权并查集)

    下面的是从该网站上copy过来的,稍微改了一点,给出链接:http://hi.baidu.com/nondes/item/26dd0f1a02b1e0ef5f53b1c7 题意:有N个人玩剪刀石头布, ...

  4. POJ 1988 Cube Stacking( 带权并查集 )*

    POJ 1988 Cube Stacking( 带权并查集 ) 非常棒的一道题!借鉴"找回失去的"博客 链接:传送门 题意: P次查询,每次查询有两种: M x y 将包含x的集合 ...

  5. poj 1733 Parity game(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 题目大意:有一个很长很长含有01的字符串,长度可达1000000000,首先告诉你字符串的长度n,再给一个m,表示给你m条信息, ...

  6. POJ 1733 Parity game(离散化+带权并查集)

    离散化+带权并查集 题意:长度为n的0和1组成的字符串,然后问第L和R位置之间有奇数个1还是偶数个1. 根据这些回答, 判断第几个是错误(和之前有矛盾)的. 思路:此题同HDU 3038 差不多,询问 ...

  7. POJ 1988 Cube Stacking 【带权并查集】

    <题目链接> 题目大意: 有几个stack,初始里面有一个cube.支持两种操作: 1.move x y: 将x所在的stack移动到y所在stack的顶部. 2.count x:数在x所 ...

  8. POJ 1733 Parity game 【带权并查集】+【离散化】

    <题目链接> 题目大意: 一个由0,1组成的序列,每次给出一段区间的奇偶,问哪一条信息不合法. 解题分析: 我们用s[i]表示前i个数的前缀和,那么a b even意味着s[b]和s[a- ...

  9. poj 1984 Navigation Nightmare(带权并查集+小小的技巧)

    题目链接:http://poj.org/problem?id=1984 题意:题目是说给你n个线,并告知其方向,然后对于后面有一些询问,每个询问有一个时间点,要求你输出在该时间点a,b的笛卡尔距离,如 ...

随机推荐

  1. [Unity官方文档翻译]Primitive and Placeholder Objects Unity原生3D物体教程

    Primitive and Placeholder Objects 原始的基础物体 Unity can work with 3D models of any shape that can be cre ...

  2. script 里写 html 模版

    js模版引擎(例如:template.js 或 handlebars.js)一般都用<script>标签来存放模版的内容 1)模版写在<script>标签和写在<div& ...

  3. 【RF库Collections测试】lists should be equal

    场景一:msg=None 场景二:自定义msg 场景三:自定义msg和values,且values为布尔类型False或者字符串False和No Values 场景四:自定义msg和values,且v ...

  4. iOS开发--打印NSRange,CGRect等结构体

    使用对应的转换NSStringFromCGPoint   NSStringFromCGSize   NSStringFromCGRect  NSStringFromCGAffineTransform  ...

  5. CentOS配制FTP服务器,并且能用root权限登录

    步骤如下: 1.运行yum install vsftpd命令 具体的细节如下:(如果无法更新,你先配置能访问互联网,我有文档叫 CentOS 在 VMware下,如何联网到Internet的解决办法可 ...

  6. vc 关于局部刷新

    在绘制图像对象的时候,时刻获取其所占范围大小,并使用InvalidateRect( m_rectRefresh);刷新,但是光这样还是不行的要在onDraw()函数里获取PAINTSTRUCT结构的无 ...

  7. Unicode编码转换汉字

    Uri.UnescapeDataString(string) #region Unicode转换汉字 Console.WriteLine(Uri.UnescapeDataString("\u ...

  8. session超时跃出iframe并跳到登陆页面(转载)

    session超时跳出iframe并跳到登陆页面 在网页编程时,我们经常需要处理,当session过期时,我们要跳到登陆页面让用户登陆,由于我们可能用到IFrame框架,所以我们我登陆页面需要显示在整 ...

  9. Git学习之Git检出

    ================================================ HEAD 的重置即检出 ======================================= ...

  10. WEB-DICT词库计划

    欢迎大家支持晓阳童鞋的词库计划,建立一个庞大的中文词库 地址如下:http://webdict.info/ 什么是WEB-DICT词库计划? WEB-DICT词表计划目标是通过机器学习算法以及人工标注 ...