题目
n个小伙伴进行猜拳有戏,除了一个比较聪明的家伙(小法官)以外,其他人只会出单一的一种,给出m中猜拳的结果,要求找出那个比较聪明的小伙伴序号,并且输出在第几次猜拳可以确定。(注意<,>,=前后可能有空格)
- 法官可能不止一个,不止一个的时候输出 Can not determine
- 法官可能不存在,不存在的时候输出 Impossible
- 法官可以确定为一个时,输出法官的编号和在最少在第几次猜拳可以确定。
题解思路
因为没有比较好的翻译加上这题意思含糊不清导致写了很久。
看了别人的题解,基本上都是枚举对所有人是否为法官,并将涉及他们的关系去除,因为当枚举到法官时,我们用带权并查集进行处理时,其他的关系全部都能用带权并查集正确推出的。
记录有几个满足的枚举数即可。
当发现关系不能被正确推出时,说明枚举的这个家伙不是法官直接记录第几次猜拳然后break,这里的至少第几次猜拳是指在枚举中全阶段中,运用到的最大的猜拳次数。每次枚举时记录更新最大值。
有关带权并查集的部分基本上是模板。
怎么处理多法官和无法官进行枚举才是这题的难点。
AC代码
#include <iostream>
#include <cstdio>
using namespace std;
int a[510];
int w[510];
struct node
{
int x,y,c;
}gx[2100];
int find2(int x )
{
if ( x != a[x] )
{
int t = a[x];
a[x] = find2(a[x]);
w[x] = (w[x] + w[t])%3;
}
return a[x];
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int x,y,c;
char s;
for ( int i = 1 ;i <= m ; i++ )
{
scanf("%d%c%d",&x,&s,&y);
gx[i].x = x;
gx[i].y = y;
if (s == '>')
gx[i].c = 2;
else if ( s == '=')
gx[i].c = 0;
else
gx[i].c = 1;
}
int flag = 0,ans = 0,as = 0;
for ( int k = 0 ; k < n; k++ )
{
for (int i = 0 ; i <= n ; i++ )
{
a[i] = i;
w[i] = 0;
}
int s = 0,te = 0,pit = 0;
for ( int i = 1 ;i <= m ; i++ )
{
s = i;
x = gx[i].x;
y = gx[i].y;
c = gx[i].c;
if (x == k || y == k )
continue;
int fx = find2(x);
int fy = find2(y);
if ( fx == fy )
{
if ( c != (w[x] - w[y] + 3)%3 )
{
te = i;
pit = 1;
break;
}
}else
{
a[fx] = fy;
w[fx] = ( -w[x] + c + 3 + w[y] )%3;
}
}
if ( s == m && pit == 0)
{
ans++;
flag = k;
}else
{
as = max(te,as);
}
}
// cout<<ans<<"\n";
if ( ans == 0 )
printf("Impossible\n");
else if ( ans == 1 )
printf("Player %d can be determined to be the judge after %d lines\n",flag,as);
else
printf("Can not determine\n");
}
return 0;
}