博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Rochambeau POJ - 2912(带权并查集+暴力枚举)
阅读量:4136 次
发布时间:2019-05-25

本文共 3054 字,大约阅读时间需要 10 分钟。

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
还是三种关系,和食物链那个题一样的思路。
对于每种关系,我们给它赋予一定的权值,=的时候为0,<的时候为1,>的时候为2.
然后我们枚举每个点作为法官,与他的关系都删除掉。然后判断剩下的关系有没有冲突,要是没有冲突,他就是法官。如果有冲突,他就不是法官。如果有多个符合条件,就说明不能判断了。如果没有符合条件的,那就是不可能的。
代码如下:

#include
#include
#include
#include
#include
#define ll long longusing namespace std;const int maxx=5e2+100;const int maxm=2e3+100;int f[maxx],dis[maxx],del[maxx];struct node{
int x,y,v;}p[maxm];int n,m;inline void init(){
for(int i=0;i<=n;i++) f[i]=i,dis[i]=0;}inline int getf(int u){
int x; if(u!=f[u]) {
x=getf(f[u]); dis[u]=(dis[u]+dis[f[u]])%3; f[u]=x; } return f[u];}inline int merge(int u,int v,int val){
int t1=getf(u); int t2=getf(v); if(t1==t2) {
if((dis[v]+val)%3!=dis[u]) return 1; else return 0; } else {
f[t1]=t2; dis[t1]=(dis[v]-dis[u]+val+3)%3; return 0; }}int main(){
int x,y;char c; while(~scanf("%d%d",&n,&m)) {
for(int i=0;i
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;}

努力加油a啊,(o)/~

转载地址:http://nutvi.baihongyu.com/

你可能感兴趣的文章