您的位置:首页 > 其它

poj 2912 Rochambeau(带权并查集)(枚举)

2017-04-01 20:16 323 查看

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 ≤
4000
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

思路:

很明显是关系并查集,他们之间的关系表达式也很容易推导出,但是裁判的确定却并不容易。

那这个时候我们观察到数据范围也不是很大,所以我们可以直接暴力枚举每一个人是否为裁判,枚举时对于涉及这个人的游戏回合都不进行并查集操作,看剩下的游戏中是否会有矛盾,如果有则说明这个人不是裁判并且此时要记录下在哪个回合出了矛盾,否则的话他就有可能是。

然后就遍历所有人看是否只有一个人没有出现矛盾,如果只有一个,则说明可以确定裁判,那么确定裁判的局数就是其他人出现矛盾的回合的最大值。因为只有否定了其他所有人,才能够确定才裁判。

代码:

#include<stdio.h>
#include<string.h>

#define maxn 500+10
#define maxv 2000+10
#define max(a,b) (a>b?a:b)
#define mem(a,b) memset(a,b,sizeof(a))

struct node
{
int u,v,k;
} q[maxv];
int pre[maxn],rank[maxn],error[maxn];
int n,m;

void init()
{
for(int i=0; i<=n; ++i)
pre[i]=i,rank[i]=0;
}

int Find(int x)
{
if(x==pre[x])
return x;
int temp=pre[x];
pre[x]=Find(temp);
rank[x]=(rank[x]+rank[temp])%3;
return pre[x];
}

int main()
{
while(~scanf("%d%d",&n,&m))
{
int i,j,k,u,v;
char op;
for(i=1; i<=m; ++i)
{
scanf("%d %c %d",&q[i].u,&op,&q[i].v);
if(op=='>')
q[i].k=2;
else if(op=='<')
q[i].k=1;
else if(op=='=')
q[i].k=0;
}
mem(error,-1);
for(i=0; i<n; ++i)
{
init();//注意这里要初始化,因为每次建立的关系都有可能不一样
for(j=1; j<=m; ++j)
{
if(q[j].u==i||q[j].v==i)
continue;
u=q[j].u,v=q[j].v,k=q[j].k;
int fx=Find(u),fy=Find(v);
if(fx!=fy)
{
pre[fy]=fx;
rank[fy]=(rank[u]+k+3-rank[v])%3;
}
else
{
if((rank[v]+3-rank[u])%3!=k)
{
error[i]=j;
break;
}
}
}
}
int cnt=0,ans=0;
for(i=0; i<n; ++i)
{
if(error[i]==-1)
++cnt,v=i;
ans=max(ans,error[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",v,ans);
}
return 0;
}


总结:

我的关系表达式是推出来了,但是裁判的确定却出了问题,我原先的想法是如果有哪个回合出了矛盾,就先判断下这个回合中的人是否已经被确定为裁判嫌疑人,如果不是那就把他们两个都设为嫌疑人,如果其中有一个人已经是嫌疑人,那么此时这个人就肯定是裁判了。如果没有嫌疑人,那就是Can not determine。如果同时出现了第三个和第四个嫌疑人,那么就肯定是impossible了。但是却wrong了0.0(不知道哪里错了)

好吧,数据小的时候要想到暴力枚举,不过并查集+枚举还是第一次做
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: