POJ2912:Rochambeau(带权并查集)
Rochambeau
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 5208 | Accepted: 1778 |
题目链接:http://poj.org/problem?id=2912
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
题意:
n个小朋友被分为三组玩剪刀石头布,每一组的小朋友只出固定的招数,但是这些小朋友里面有个裁判,他可以任意出,现在问是否能够确定出这个裁判,最少经过几轮?
题解:
三个组并且是个关于输赢的环状关系,所以我们可以思考带权并查集,v[x]代表x与其父亲结点的关系,v[x]=0代表同类,v[x]=1代表x赢他父亲,v[x]=2就代表输了。
现在关键是这个裁判,我们通过思考可以发现,假如确定了这个人为裁判,那么他参与的几次都不算做分组。
但如何去确定就是个问题。注意这题时间限制挺大的,所以我们直接枚举,假定每个人都作为裁判就行了。
最后还要确定经过几轮游戏,这里挺有意思的:
确定目前这个人为裁判,这等价于排除其他所有人不是裁判的最少次数,这个在枚举其他人作为裁判时就可以进行处理了。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std; const int N = ;
int n,m;
int f[N],a[N],b[N],v[N],ans[N];
char c[N]; int find(int x){
if(x==f[x]) return x;
int tmp=f[x];
f[x]=find(f[x]);
v[x]=(v[x]+v[tmp])%;
return f[x];
} int main(){
while(~scanf("%d%d",&n,&m)){
for(int i=;i<=m;i++) scanf("%d%c%d",&a[i],&c[i],&b[i]);
int tot = ,cnt = ,l = ;
memset(ans,,sizeof(ans));
for(int i=;i<n;i++){
bool flag=true;cnt=;
for(int j=;j<=n;j++) f[j]=j,v[j]=;
for(int j=;j<=m;j++){
if(a[j]==i || b[j]==i) continue;
int fx=find(a[j]),fy=find(b[j]);
if(c[j]=='>'){
if(fx==fy && (v[a[j]]+-v[b[j]])%!=) flag=false,cnt=j;
else{
f[fx]=fy;
v[fx]=(-v[a[j]]++v[b[j]])%;
}
}else if(c[j]=='<'){
if(fx==fy && (v[a[j]]+-v[b[j]])%!=) flag=false,cnt=j;
else{
f[fx]=fy;
v[fx]=(-v[a[j]]++v[b[j]])%;
}
}else{
if(fx==fy && (v[a[j]]+-v[b[j]])%!=) flag=false,cnt=j;
else{
f[fx]=fy;
v[fx]=(-v[a[j]]+v[b[j]])%;
}
}
if(!flag) break;
}
if(flag) ans[++tot]=i;
l=max(l,cnt);
}
if(tot==) puts("Impossible");
else if(tot>) puts("Can not determine");
else printf("Player %d can be determined to be the judge after %d lines\n",ans[],l);
}
return ;
}
POJ2912:Rochambeau(带权并查集)的更多相关文章
- POJ - 2912 Rochambeau (带权并查集+枚举)
题意:有N个人被分为了三组,其中有一个人是开了挂的.同组的人的关系是‘=’,不同组的人关系是‘<’或'>',但是开了挂的人可以给出自己和他人任意的关系.现在要根据M条关系找出这个开了挂的人 ...
- poj2912(带权并查集+枚举)
题目链接:http://poj.org/problem?id=2912 题意:给n个人,m组关系,玩石头剪刀布的游戏,n个人中除一个人judge以外,其他人属于3个group(即石头.剪刀.布),他们 ...
- POJ 2912 Rochambeau(难,好题,枚举+带权并查集)
下面的是从该网站上copy过来的,稍微改了一点,给出链接:http://hi.baidu.com/nondes/item/26dd0f1a02b1e0ef5f53b1c7 题意:有N个人玩剪刀石头布, ...
- [poj 2912] Rochambeau 解题报告 (带权并查集)
题目链接:http://poj.org/problem?id=2912 题目: 题目大意: n个人进行m轮剪刀石头布游戏(0<n<=500,0<=m<=2000) 接下来m行形 ...
- POJ 1703 Find them, Catch them(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
- [NOIP摸你赛]Hzwer的陨石(带权并查集)
题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...
- poj1417 带权并查集 + 背包 + 记录路径
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2713 Accepted: 868 Descrip ...
- poj1984 带权并查集(向量处理)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5939 Accepted: 2 ...
- 【BZOJ-4690】Never Wait For Weights 带权并查集
4690: Never Wait for Weights Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 88 Solved: 41[Submit][ ...
随机推荐
- RubyMine常用快捷键
一级必会 Shift+F10:运行running Ctrl+Alt+R:弹出RakeCtrl+Alt+G:弹出GenerateCtrl+Alt+L:格式化代码Alt+F1:切换视图(Project, ...
- BGP(边界网关协议)简述
BGP的起源 不同自治系统(路由域)间路由交换与管理的需求推动了EGP的发展,但是EGP的算法简单,无法选路,从而被BGP取代. 自治系统:(AS) IGP:自治系统内部协议,ospf,rip,is- ...
- 521. [NOIP2010] 引水入城 cogs
521. [NOIP2010] 引水入城 ★★★ 输入文件:flow.in 输出文件:flow.out 简单对比时间限制:1 s 内存限制:128 MB 在一个遥远的国度,一侧是风景秀 ...
- FPGA数字鉴相鉴频器的开发记录
1. 对于电机的锁相控制,需要对相差进行PI性质的环路滤波,但现有的锁相环中鉴频鉴相器输出为相差脉冲而非数字量,难以直接进行PI特性的环路滤波. 通过对晶振的非整数分频获取准确的参考时钟,基于触发器机 ...
- es2017中的async和await要点
1. async和await最关键的用途是以同步的写法实现了异步调用,是对Generator异步方法的简化和改进.使用Generator实现异步的缺点如下: 得有一个任务执行器来自动调用next() ...
- 一个知乎日报pwa
前几天写了一篇文章关于如何实现一个简单版的pwa应用,端午撸了一个简易版知乎日报pwa. 关于如何写一个pwa,这里就不多介绍了,请移步这里.应用使用vue+vuex+axios,API这里,这里做了 ...
- 「日常训练」「小专题·USACO」 Broken Necklace(1-2)
题意 圆形链条,打断一处可以形成一条链.问在哪个地方开始打断,能够形成最大的连续颜色(白色视作同样的颜色)? 分析 说起来很高级,但是我们实际上并不需要穷举打断的地方,只需要把串重复三回啊三回.然后从 ...
- MySQL☞between ... and ...
between 初值 and 终值:求出该列列值在初值和终值之间所有的数据 格式如下: select 列名/* from 表名 where 列名 between 初值 and 终值 如下图:
- APP功能性测试-3
定义:兼容测试就是指软件在特定的硬件平台,不同的应用软件之间,不同的操作系统平台上,不同的网络等环境中是否能够正常的运行的测试 (会不会产生不兼容) 兼容性测试的作用 进一步提高产品质量 和其他软件 ...
- 基于Ubuntu搭建Linux路由器
开源,几乎代表了无所不能的意思,最近又因为它玩Hi了... 因业务发展,需要临时接入300MB的专线和千兆路由器,而公司现有的路由器却是百兆的,出于成本考虑,只能不想更换新的路由器,在网上查了一下可以 ...