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.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”(一个正整数值)来表示 ...
随机推荐
- luogu3380 【模板】二逼平衡树(树套树)
#include <iostream> #include <cstdlib> #include <cstdio> #include <ctime> us ...
- Git上传的使用步骤
Git上传的使用步骤 首先 git branch 查看当前的分支是否为本地自己分支 接着 git stash 保存本地自己的保存 git checkout earemote 查看本地共有开发分支 gi ...
- 什么样的经历,才能领悟成为架构师? >>>
什么样的经历,才能领悟成为架构师? >>> 本文主要分析 SpringBoot 的启动过程. SpringBoot的版本为:2.1.0 release,最新版本. 一.时序图 还是老 ...
- 两行代码搞定UI主流框架
XCNavTab XCNavTab适用于快速搭建NavigationController和TabBarController相结合的框架 https://github.com/xiaocaiabc/XC ...
- Codevs 2756 树上的路径
2756 树上的路径 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给出一棵树,求出最小的k,使得,且在树中存在 ...
- ES6__class 的继承等相关知识案例
/** * class 的继承等相关知识 */ // extends. static. super const canvas = document.querySelector('#canvas'); ...
- Delphi控件大全
首先来大体上为控件分一下类,以方便我们后面的讨论. 但因为控件的种类太多,所以就粗略的分为如下几个类别∶ ---界面风格类 ---Shell外观类 ---Editor类 ---Gr ...
- React学习及实例开发(一)——开始
本文基于React v16.4.1 初学react,有理解不对的地方,欢迎批评指正^_^ 一.构建一个新项目 1.命令行运行如下命令,构建一个新的react项目 npm install -g crea ...
- 实现浏览器兼容的innerText
今天学习到了FF不支持innerText,而IE.chrome.Safari.opera均支持innerText. 为了各个浏览器能兼容innerText,必须对js做一次封装. 为啥能实现兼容呢?原 ...
- jmeter的jmx脚本结构解析
jmeter的jmx脚本是xml文档,简单分析下其结构 xml是树形结构:jmeter界面的树形结构就是xml的结构 一级目录: 二级目录:在一级目录右键后可以看到的,都可以做为二级目录 三级目录.n ...