Rochambeau POJ - 2912(带权并查集+暴力枚举)

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<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
#define ll long long
using 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<m;i++)
		{
			scanf("%d%c%d",&x,&c,&y);
			if(c=='=') p[i].v=0;
			else if(c=='<') p[i].v=1;
			else p[i].v=2;
			p[i].x=x,p[i].y=y;
		}
		memset(del,-1,sizeof(del));
		for(int i=0;i<n;i++)//暴力枚举
		{
			init();
			for(int j=0;j<m;j++)
			{
				if(p[j].x==i||p[j].y==i) continue;
				if(merge(p[j].x,p[j].y,p[j].v))
				{
					del[i]=j;
					break;
				}
			}
		}
		int ans1=-1,ans2=0,cnt=0;
		for(int i=0;i<n;i++)
		{
			if(del[i]==-1)//说明删掉这个点其余的点都是没有冲突的,那么这个点就是法官
			{
				cnt++;
				ans1=i;
			}
			ans2=max(ans2,del[i]+1);
		}
		if(cnt==0) 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;
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值