POJ2912 Rochambeau —— 种类并查集 + 枚举
题目链接:http://poj.org/problem?id=2912
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 3663 | Accepted: 1285 |
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 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
Source
Chen, Shixi (xreborner) living in http://fairyair.yeah.net/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e3+; int n, m;
int fa[MAXN], r[MAXN];
struct node
{
int u, v, w;
}a[]; int find(int x)
{
if(fa[x]==-) return x;
int pre = find(fa[x]);
r[x] = (r[x]+r[fa[x]])%;
return fa[x] = pre;
} bool Union(int u, int v, int w)
{
int fu = find(u);
int fv = find(v);
if(fu==fv)
return ((-w+r[u])%!=r[v]); fa[fu] = fv;
r[fu] = (-r[u]+w+r[v])%;
return false;
} int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
for(int i = ; i<=m; i++)
{
char ch;
scanf("%d%c%d", &a[i].u, &ch, &a[i].v);
if(ch=='=') a[i].w = ;
if(ch=='<') a[i].w = ;
if(ch=='>') a[i].w = ;
} int judge, index = , cnt = ; //judge为裁判的下标, index为找到裁判时的下标, cnt为可能是裁判的人的个数。
for(int k = ; k<n; k++) //枚举每一个人作为裁判
{
bool flag = true;
memset(fa, -, sizeof(fa));
memset(r, , sizeof(r));
for(int i = ; i<=m; i++)
{
int u = a[i].u, v = a[i].v, w = a[i].w; //其中一个人是“裁判”, 则不作处理
if(u==k || v==k) continue; if(Union(u, v, w)) //出现冲突,排除这个人是裁判
{
/**找到裁判时的下标,取所有发生冲突时,下标的最大值。为什么?
当排除完其他人都不是裁判时,裁判是谁就自然显露出来了 **/
index = max(index, i);
flag = false;
break;
}
} if(flag) //如果跳过了k后,其他人的关系都不会发生冲突, 那么k就有可能是裁判
{
judge = k;
cnt++; //更新个数
}
} if(cnt==) //可能是裁判的人的个数为0:谁都不可能是裁判
printf("Impossible\n");
else if(cnt>) //可能是裁判的人的个数大于1:不能确定谁是裁判
printf("Can not determine\n");
else //可能是裁判的人的个数等于1:这个人就是裁判
printf("Player %d can be determined to be the judge after %d lines\n", judge, index);
}
}
POJ2912 Rochambeau —— 种类并查集 + 枚举的更多相关文章
- poj2912(种类并查集+枚举)
题目:http://poj.org/problem?id=2912 题意:n个人进行m轮剪刀石头布游戏(0<n<=500,0<=m<=2000),接下来m行形如x, y, ch ...
- POJ2912 Rochambeau [扩展域并查集]
题目传送门 Rochambeau Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4463 Accepted: 1545 ...
- POJ - 2912 Rochambeau 种类并查集
题意:有三组小朋友在玩石头剪刀布,同一组的小朋友出的手势是一样的.这些小朋友中有一个是裁判,他可以随便出手势.现在给定一些小朋友的关系,问能否判断出裁判,如果能最早什么时候能够找到裁判. 思路:枚举每 ...
- [POJ2912]Rochambeau(并查集)
传送门 题意: n个人分成三组,玩石头剪子布游戏,同一组的人只能出同样固定的的手势,其中有一个是裁判不属于任何组,可以出任意手势,给出m个信息x op y 表示x,y是从三个组里面随机抽取的或者是裁判 ...
- POJ 2912 Rochambeau(种类并查集+枚举)
题目链接:http://poj.org/problem?id=2912 题目大意:n个人玩,玩石头剪刀布游戏,其中1人是裁判,剩下的n-1个人分为3组, 他们商量好了,相同组的人每次都出相同的手势,不 ...
- 洛谷 P1525 【关押罪犯】种类并查集
题解 P1525 [关押罪犯]:种类并查集 前言: 在数据结构并查集中,种类并查集属于扩展域并查集一类. 比较典型的题目就是:食物链(比本题难一些,有三个种类存在) 首先讲一下本题的贪心,这个是必须要 ...
- 洛谷 P1525 关押罪犯 & [NOIP2010提高组](贪心,种类并查集)
传送门 解题思路 很显然,为了让最大值最小,肯定就是从大到小枚举,让他们分在两个监狱中,第一个不符合的就是答案. 怎样判断是否在一个监狱中呢? 很显然,就是用种类并查集. 种类并查集的讲解——团伙(很 ...
- NOI2001|POJ1182食物链[种类并查集 向量]
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65430 Accepted: 19283 Description ...
- NOIP2010关押罪犯[并查集|二分答案+二分图染色 | 种类并查集]
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”(一个正整数值)来表示 ...
随机推荐
- 【ORACLE】查看死锁进程并结束死锁的脚本
--共享锁:Share:排他锁:Exclusive:行共享锁:Row-S:行排他锁:Row-X select V$SESSION.sid,v$session.SERIAL#,v$process.spi ...
- 一份快速实用的 tcpdump 命令参考手册
对于 tcpdump 的使用,大部分管理员会分成两类.有一类管理员,他们熟知 tcpdump 和其中的所有标记:另一类管理员,他们仅了解基本的使用方法,剩下事情都要借助参考手册才能完成.出现这种情况 ...
- POJ1061青蛙的约会
Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...
- 巴蜀3540 -- 【Violet 6 最终话】蒲公英
Description 原题的时间限制是 2s . 亲爱的哥哥: 你在那个城市里面过得好吗? 我在家里面最近很开心呢.昨天晚上奶奶给我讲了那个叫「绝望」的大坏蛋的故事的说!它把人们的房子和田地搞坏,还 ...
- Java文件内容读写
package regionForKeywords; import java.io.*; /** * Created by huangjiahong on 2016/2/25. */ public c ...
- hdu 5200 Trees [ 排序 离线 2指针 ]
传送门 Trees Accepts: 156 Submissions: 533 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 655 ...
- Codeforces Round #297 (Div. 2) D. Arthur and Walls [ 思维 + bfs ]
传送门 D. Arthur and Walls time limit per test 2 seconds memory limit per test 512 megabytes input stan ...
- 给Ubuntu更换成163的源(sources.list)Unable to locate package
Refer to http://www.crifan.com/ubuntu_change_sources_list_to_163/ 1. backup /etc/apt/sources.list 2. ...
- HDU 6437 最(大) 小费用最大流
Problem L.Videos Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- P1194 买礼物 洛谷
https://www.luogu.org/problem/show?pid=1194 题目描述 又到了一年一度的明明生日了,明明想要买B样东西,巧的是,这B样东西价格都是A元. 但是,商店老板说最近 ...