poj 2912 Rochambeau(带权并查集)
2015-09-08 14:10
281 查看
题目:http://poj.org/problem?id=2912
Rochambeau
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
Sample Output
分析:大意是,许多人分成三组一起来玩剪刀石头布,有一个人是judge,可以任意出,相同组的人必须出一样,问哪一个是judge。这需要枚举,并且和食物链那个问题很相似。这里假设一个人是judge,把他排除,如果结果只有一个矛盾那么他就是judge(这个矛盾就是一个0,1,2的循环);如果有多个矛盾(多个循环,1,2;2,1;0,1;1,0,;0,1,2等)那么他不是judge,无法判断;如果没有一个循环,那是不可能的(Impossible)。
Rochambeau
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 2293 | Accepted: 822 |
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
分析:大意是,许多人分成三组一起来玩剪刀石头布,有一个人是judge,可以任意出,相同组的人必须出一样,问哪一个是judge。这需要枚举,并且和食物链那个问题很相似。这里假设一个人是judge,把他排除,如果结果只有一个矛盾那么他就是judge(这个矛盾就是一个0,1,2的循环);如果有多个矛盾(多个循环,1,2;2,1;0,1;1,0,;0,1,2等)那么他不是judge,无法判断;如果没有一个循环,那是不可能的(Impossible)。
#include <iostream> #include <cstdio> #include <cstring> using namespace std; struct Node { int u,v,k; }a[2005]; int n,m; int pre[505],r[505]; int error[505]; char str[1000]; int find(int x) { if(x==pre[x]) return x; int f=pre[x]; pre[x]=find(pre[x]); r[x]=(r[x]+r[f])%3; return pre[x]; } int main() { //freopen("cin.txt","r",stdin); while(scanf("%d%d",&n,&m)!=EOF) { getchar(); for(int i=0;i<m;i++) { gets(str); int q1=0,q2=0,tag,len=strlen(str); for(int j=0;j<len;j++){ if(str[j]=='>'||str[j]=='<'||str[j]=='='){ tag=j; break; } q1=q1*10+str[j]-'0'; } for(int j=tag+1;j<len;j++) q2=q2*10+str[j]-'0'; a[i].u=q1; a[i].v=q2; if(str[tag]=='=') a[i].k=0; else if(str[tag]=='<') a[i].k=1; else a[i].k=2; //cout<<a[i].u<<" "<<a[i].k<<" "<<a[i].v<<endl; } memset(error,-1,sizeof(error)); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { pre[j]=j; r[j]=0; //不要设成-1啊,想想食物链 } for(int j=0;j<m;j++) { if(i==a[j].u||i==a[j].v) continue; int fx=find(a[j].u),fy=find(a[j].v); if(fx==fy) { int c=a[j].k; if((r[a[j].u]+c)%3!=r[a[j].v]) { error[i]=j+1; break; } } else { pre[fy]=fx; r[fy]=(r[a[j].u]-r[a[j].v]+3+a[j].k)%3; //+r[fx] } } } int sum=0,p1=0,p2=0; for(int i=0;i<n;i++) { if(error[i]==-1) sum++, p1=i; p2=max(p2,error[i]); // 1: error[i]=-1,-1,-1,3,-1,-1,…… } if(sum==0) puts("Impossible"); else if(sum>1) puts("Can not determine"); else printf("Player %d can be determined to be the judge after %d lines\n",p1,p2); } return 0; }
相关文章推荐
- vi 替换技巧
- First Bad Version 第一个坏数 二分查找
- 将炙手可热的敏捷方法论应用到市场营销团队当中
- 一步一步开发Game服务器(四)地图线程
- [NOIP 2013]华容道
- LayoutControl 布局控件
- Java中Static Nested Class 和 Inner Class的不同
- Mybstis insert操作详解及在使用过程中遇到的问题
- linux ln 命令使用参数详解(ln -s 软链接)
- Android apk动态加载机制的研究(二):资源加载和activity生命周期管理
- java中的Maven项目的两种打包方法
- H-Index 引用次数 排序
- android Telephony结构
- 开发注意事项
- ArcGIS JavaScript API异常之onExtentChange事件覆盖onClick事件
- 正则表达式-断言 作者deerchao
- 数据结构之单向链表操作1-(插入,删除,交换,反转,排序等操作)
- 【Python基础】Python面向对象 - 5 - 元类
- java socket内容回顾
- 一次mysql的查询索引的优化