题目链接: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. SpringMVC由浅入深day01_5注解的处理器映射器和适配器

    5 注解的处理器映射器和适配器 在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandle ...

  2. Mybatis -- 批量更新 -- updateBatch

    mysql数据库配置: 数据库连接必须配置:&allowMultiQueries=true并且‘&’ 用&替换 jdbc.url=jdbc:mysql://192.168.10 ...

  3. 8 -- 深入使用Spring -- 3...1 Resource实现类ClassPathResource

    8.3.1 Resource实现类------ClassPathResource : 访问类加载路径下的资源的实现类 2.访问类加载路径下的资源 ClassPathResource 用来访问类加载路径 ...

  4. 关于openssl的编译与使用

    关于openssl的编译与使用,可以参考这两往篇文章 http://blog.csdn.net/lazyclough/article/details/7456131 http://www.leaves ...

  5. [Vim] 搜索模式(正则表达式)

    本文介绍如何使用Vim的搜索模式. 搜索单词 Vim中使用 \< 和 \> 分别表示单词的开头和结尾,例如查找单词 i 而不是字母 i ,在正常模式下,按下 / 启动搜索模式,输入 \&l ...

  6. Illegal mix of collations for operation 'concat'

    在t_employee表中,练习使用concat函数连接字符串时, mysql> select concat('工号为:',fnumber,'的员工的幸福指数:',fsalary/(fage-2 ...

  7. MyBatis中Like语句使用总结

    原生写法 eg: select * from user where username like '%${value}%' 注意:     ${value}里面必须要写value,不然会报错 oracl ...

  8. U盘安装centos6.4:缺少iso 9660映像

    方法: 1.下载安装的ISO文件    到www.centos.org网站下载对应的Centos 6.4安装文件.下载站点我一般选择网易镜像站点    64位下载参考链接:http://mirrors ...

  9. linux prefix

    指定安装路径不指定prefix,则可执行文件默认放在/usr /local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc.其它的资源文件放在/usr ...

  10. Git学习之msysGit环境支持

    ============================== msysGit中Shell环境的中文支持 ============================== 1 中文录入的问题 默认的Shel ...