Poj-2912 Rochambeau 枚举+并查集

题目链接

题意

  给出n种关系,有以下三种情况:

    a>b :a赢了b

    a<b :b赢了a

    a=b :a与b平手

  现在有三组人,出的手势是一定的,但是其中有一名judge,他可以出任意手势。问通过哪一行就可以推断出谁是judge。若没有则输出impossible。若有多个,则输出can not determine

分析

  1.如何确定Judge

  因为Judge可以任意出手势,所以凡是有关judge所建立起的关系都是不可靠的。

  因此,我们可以枚举judge,凡是跟judge有关的关系都不考虑。如果此时建立起的关系有矛盾,则说明现在枚举到得人不是judge。

  若有矛盾,则记录下在哪句话矛盾。此时,这句话即为判断该人不是judge的一行。

     能判断出谁的裁判的那行即为其他不是裁判点都在该行前出现冲突,故该行即为上述出现冲突行的最大值。

  若有若干个人枚举后无矛盾,即这些人作为judge时均无矛盾,则can not determine。

 



#include "stdio.h"
#include "map"
#include "queue"
#include "iostream"
#include "functional"
#include "math.h"
#include "algorithm"
using namespace std;
const int maxn = 2005;
const int mod = 1000000007 ;
const int inf = 1<<30;
typedef __int64 LL;
typedef pair<int,int> pii;
int n,m;
int p[maxn],r[maxn];
struct Node
{
	int a,b,g;
}que[maxn];
int find( int x )
{
	if( p[x] == x )	return x;
	int tmp = p[x];
	p[x] = find( p[x] );
	r[x] = ( r[x] + r[tmp] )%3;
	return p[x];
}
bool merge( int a,int b,int d )
{
	int x = find( a );
	int y = find( b );
	if( x != y )
	{
		p[y] = x;
		r[y] = ( r[a] + (3 - r[b]) + d-1 )%3;
		return true;
	}
	return false;
}
void init()
{
	for( int i = 0; i <= n; i ++ )
	{
		p[i] = i;
		r[i] = 0;
	}
}
int main()
{
	#ifndef ONLINE_JUDGE     
	freopen("data.txt","r",stdin);     
	#endif
	char ch;
	while( scanf("%d%d",&n,&m) != EOF )
	{
		for( int i = 1; i <= m; i ++ )
		{
			scanf("%d%c%d",&que[i].a,&ch,&que[i].b);
			if( ch == '=' )	que[i].g = 0;
			else if( ch == '>' )	que[i].g = 2;
			else if( ch == '<' )	que[i].g = 1;
		}
		int flag = 0,ok,line = 0,judge = 0;
		for( int i = 0; i < n; i ++ )
		{
			init(); ok = 1;
			for( int j = 1; j <= m; j ++ )
			{
				if( que[j].a != i &&  que[j].b != i ){
					int fx = find( que[j].a );
					int fy = find( que[j].b );
					if( fx == fy )
					{
						if( ( r[que[j].a]+ que[j].g )%3 != r[que[j].b]  )
						{
							ok = false;
							line = max( line,j );
							break;
						}
					}
					else
					{
						p[fy] = fx;
						r[fy] = ( r[fx] + r[que[j].a] + 3 -  r[que[j].b] + que[j].g )%3;
					}
				}
			}
			if( ok ){	
				if( ++flag > 1 )	break;
				judge = i;
			}
		}
		if( flag == 1 )
			printf("Player %d can be determined to be the judge after %d lines\n",judge,line);
		else if( flag == 0 )	printf("Impossible\n");
		else printf("Can not determine\n");
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章