# POJ 2912 Rochambeau（并查集）

### 代码部分


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <string>
#include <map>
using namespace std;
#define LL long long
#define inf 0x3f3f3f3
const int N = 1005;
int pre[N];  ///记录集合序号
int val[N]; ///权值
int n,m,ans;
bool fa;
int Max;  ///最大错误步数
struct node
{
int x,y,k;
}a[N*2];
void init() ///初始化
{
for (int i = 0; i < n; i++)
{
pre[i] = i;
val[i] = 0;
}
ans = 0 ;
fa = true;
}
int fin(int x)
{
int k = pre[x];
if (x != pre[x])
{
pre[x] = fin(pre[x]);
val[x] = (val[x]+val[k])%3;
}
return pre[x];
}
void join(int x,int y,int k)
{
int fx = fin(x);
int fy = fin(y);
if (fy == fx && (val[x]+k)%3 != val[y])  ///不满足
fa = false;
else
{
pre[fy] = fx;
val[fy] = (val[x]-val[y]+k+3)%3;  ///权值更新
}
}
int main()
{
while (scanf("%d %d",&n,&m)!=EOF)
{
int x,y,k,t=0;
Max = 0;
char c;
for (int i = 0; i < m; i++)
{
scanf("%d%c%d",&a[i].x,&c,&a[i].y);
if (c == '=')
a[i].k = 0;
else if (c == '>')
a[i].k = 1;
else a[i].k = 2;
}
for (int i = 0; i < n; i++)  ///遍历每个人
{
init();
for (int j = 0; j < m;j++)
{
if (fa)  ///计算错误在第几步
ans++;
if (a[j].x == i || a[j].y == i)  ///排除带i的边
continue;
join(a[j].x,a[j].y,a[j].k);
}
if (fa)
{
t++;  ///判断可以为裁判的个数
k = i;
}
else
{
if (ans > Max)
Max = ans;
}
}
if (t > 1)
printf("Can not determine\n");
else if (t == 0)
printf("Impossible\n");
else printf("Player %d can be determined to be the judge after %d lines\n",k,Max);
}
return 0;
}



• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120