POJ 2912 Rochambeau 带权并查集

一、内容

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

二、思路

在这里插入图片描述

三、代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 505, M = 2005;
struct Node {
	int x, y;
	char c;
} r[M];
int n, m, x, y, p[N], d[N], pos[N];
int find(int x) {
	if (x == p[x]) return x;
	int root = find(p[x]);
	d[x] += d[p[x]];
	p[x] = root;
	return p[x];
}
bool merge(int x, int y, int op) {
	int fx = find(x), fy = find(y);
	if (fx == fy && (d[x] - d[y] - op) % 3) return false;
	else if (fx != fy) { //进行合并 
		p[fx] = fy; d[fx] = d[y] - d[x] + op;
	} 
	return true;
}
int main() {
	while (~scanf("%d%d", &n, &m)) {
		memset(pos, 0, sizeof(pos));
		for (int i = 1; i <= m; i++) {
			scanf("%d%c%d", &r[i].x, &r[i].c, &r[i].y);
			if (r[i].c == '<') swap(r[i].x, r[i].y);//默认x > y 
		}
		for (int i= 0; i < n; i++) {//枚举judge 
			for (int j = 0; j < n; j++) p[j] = j, d[j] = 0;
			for (int j = 1; j <= m; j++) {
				int x = r[j].x, y = r[j].y;
				if (x == i || y == i) continue; //跳过i相关的语句
				if (!merge(x, y, r[j].c != '=')) {
					pos[i] = j; 
					break; //代表出现矛盾 i不可能是judge 
				}
			} 
		}
		int cnt = 0, ans = 0, id;
		for (int i = 0; i < n; i++) {
			if (!pos[i]) {
				cnt++; id = i;
			}
			ans = max(ans, pos[i]); //获取最大一个人的break位置 
		}
		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", id, ans);
	}
	return 0;
}
发布了485 篇原创文章 · 获赞 495 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_41280600/article/details/104615244