题目链接: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. Linq To EF (添加记录后获取添加的自增ID和叫“ID”的列不是自增列不让插入的问题)

    1:添加记录后,如何获取新添加的ID的值 比如,一个实体 TestEntity   对应一个表TestEntity(ID主键自增,Name,age),使用linq to ef   添加一条记录后,如何 ...

  2. NHibernate 集合映射深入 (第五篇) <set>,<list>,<map>,<bag>

    一.集合外键 在NHibernate中,典型的用于映射集合类的元素有<set>,<list>,<map>,<bag>,<array>,< ...

  3. 8 -- 深入使用Spring -- 1...两种后处理器

    8.1 两种后处理器 Spring框架提供了很好的扩展性,出了可以与各种第三方框架良好整合外,其IoC容器也允许开发者进行扩展,这种扩展甚至无须实现BeanFactor或ApplicationCont ...

  4. mongodb常用操作命令(待续)

    1. 开启mongodb命令 >mongo 默认链接到test数据库 2. 显示所有数据库>show dbs 3.切换数据库>use 数据库名 4.查找数据库里某张表的所有成员> ...

  5. Ansible Playbook 使用条件判断语句

    先介绍一下 gather_facts 参数,该参数用于指定在执行任务前,是否先执行 setup 模块获取主机相关信息,以便给后面的任务使用 [root@localhost ~]# ansible 19 ...

  6. SaltStack 使用 Jinja2 模板

    Jinja2 是基于 python 的一个模板引擎,如下,使用 Jinja2 实现根据不同的操作系统分发不同的文件: [root@localhost ~]$ cat /srv/salt/test.sl ...

  7. std::string与std::wstring互相转换

    作者:zzandyc来源:CSDN原文:https ://blog.csdn.net/zzandyc/article/details/77540056 版权声明:本文为博主原创文章,转载请附上博文链接 ...

  8. C语言实现字符串IP与整数型IP的相互转换

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <malloc.h&g ...

  9. 【cs229-Lecture13】高斯混合模型

    本节内容: 1.混合高斯模型: 2.将混合高斯模型应用到混合贝叶斯模型:(应用:文本聚类) 3.结合EM算法,讨论因子分析算法: 4.高斯分布的有用性质. 混合高斯模型 将一般化的EM算法流程(下载笔 ...

  10. 10分钟10行代码开发APP(delphi 应用案例)

    总结一下用到的知识(开发环境安装配置不计算在内): 第六章  使用不同风格的按钮: 第十七章  让布局适应不同大小与方向的窗体: 第二十五章 使用 dbExpress访问 InterBase ToGo ...