Rochambeau(带权并查集)
来源:互联网 发布:淘宝页面改版 编辑:程序博客网 时间:2024/05/17 23:19
Rochambeau
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 3180 Accepted: 1109
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
Baidu Star 2006 Preliminary
Chen, Shixi (xreborner) living in http://fairyair.yeah.net/
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 3180 Accepted: 1109
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
Baidu Star 2006 Preliminary
Chen, Shixi (xreborner) living in http://fairyair.yeah.net/
按照食物链那题的做法,定 0,1,2 做为三种手势就可以了,不过由于判断是哪个人比较麻烦,不知道哪个人的选择是无效的,就不知道那几次游戏是不正确不能加入并查集的,因此就直接暴力枚举每个人是否为特殊的人,对于涉及这个人的所有游戏都不进行并查集操作,看剩下的游戏中是否会有矛盾,如果有就说明这个人不是特殊的人,否则他就可能是。最后如果只有一个人可能是,就可以判读出来,而判断出来的局数,就是枚举其他所有人出现矛盾的游戏场次的最大值,因为只有否定完其他所有人才能确定这个人是特殊的。
#include<stdio.h>#include<string.h>const int maxm=505;int fa[maxm],num[maxm],wa[maxm];struct ques{ int a,b,c;}q[2005];void init(int n){ for(int i=0;i<=n;++i){ fa[i]=i; num[i]=0; }}int find(int x){ int r=x,t1,t2,c=0; while(r!=fa[r]){ c+=num[r]; r=fa[r]; } while(x!=r){ t1=fa[x]; t2=c-num[x]; num[x]=c%3; fa[x]=r; c=t2; x=t1; } return r;}int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF){ for(int i=1;i<=m;++i){ char c; scanf("%d",&q[i].a); c=getchar(); while(c!='='&&c!='<'&&c!='>')c=getchar(); if(c=='=')q[i].c=0; else if(c=='<')q[i].c=1; else if(c=='>')q[i].c=2; scanf("%d",&q[i].b); } memset(wa,-1,sizeof(wa)); for(int i=0;i<n;++i){ init(n); for(int j=1;j<=m;++j){ if(q[j].a==i||q[j].b==i)continue; int x=find(q[j].a),y=find(q[j].b); if(x!=y){ fa[x]=y; num[x]=((num[q[j].b]+q[j].c-num[q[j].a])%3+3)%3; } else{ if((num[q[j].b]+q[j].c)%3!=num[q[j].a]){wa[i]=j;break;} } } } int cnt=0,ans1,ans2=0; for(int i=0;i<n;++i){ if(wa[i]==-1){ cnt++; ans1=i; } if(wa[i]>ans2)ans2=wa[i]; } if(!cnt)printf("Impossible\n"); else if(cnt>1)printf("Can not determine\n"); else printf("Player %d can be determined to be the judge after %d lines\n",ans1,ans2); } return 0;}
0 0
- Rochambeau(带权并查集)
- POJ 2912 Rochambeau(枚举+带权并查集)
- poj 2912 Rochambeau(带权并查集 + 暴力)
- poj 2912 Rochambeau(带权并查集)
- POJ - 2912 Rochambeau(带权并查集+暴力)
- POJ 2912 Rochambeau(带权并查集)
- poj 2912 Rochambeau(带权并查集)(枚举)
- 并查集-POJ2912 Rochambeau
- POJ 2912 Rochambeau(枚举+并查集)
- POJ 2912 Rochambeau(枚举 + 偏移量并查集)
- POJ 2912 Rochambeau(枚举+加权并查集)
- poj 2912 - Rochambeau(并查集)
- poj 2912 Rochambeau(暴力+并查集)
- Poj-2912 Rochambeau 枚举+并查集
- POJ 2912 Rochambeau 并查集+枚举
- POJ-2912-Rochambeau [并查集][枚举]
- POJ 2912 Rochambeau 带权并差集(食物连变形)
- 7_4_C题 Rochambeau题解 [poj 2912](并查集)
- android自动化测试Monkeyrunner源码分析之三
- okHttp封装2
- android中的小知识
- JAVA高并发 无锁(CAS)
- vw,vh视区完全覆盖与纯CSS弹框
- Rochambeau(带权并查集)
- 使用boost线程池(多线程使用opencv处理图片)
- 红外遥控波形解码1(基于正点原子战舰开发板)
- 寻址方式
- 接口与抽象类的异同点
- 测试mongodb搭建(采用主从架构)
- QiYuAdmin-在线演示(重新买了一个阿里云服务器啦)
- 枚举法(穷举法)
- 阶乘后面0的数量