POJ 2236

还是并查集,只要逐一计算距离并比较即可

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

typedef struct Node{
	int x, y;
}Node;
Node node[1010];
int flag[1010];
int p[1010];

void init() {
	for(int i = 0; i < 1010; i++) {
		p[i] = i;
	}
}

int find(int x) {
	return p[x] == x ? x : (p[x] = find(p[x]));
}

int main() {
	int n, d;
	scanf("%d%d", &n, &d);
	init();
	memset(flag, 0, sizeof(flag));
	for(int i = 1; i <= n; i++) {
		scanf("%d%d", &node[i].x, &node[i].y);
	}
	char cmd[4];
	while(scanf("%s", cmd) != EOF) {
		int a, b;
		if('O' == cmd[0]) {
			scanf("%d", &a);
			flag[a] = 1;
			for(int j = 1; j <= n; j++) {
				int ans = (node[j].x-node[a].x)*(node[j].x-node[a].x);
				ans += (node[j].y-node[a].y)*(node[j].y-node[a].y);
				if(ans <= d*d && flag[j] == 1) {
					int t1 = find(a);
					int t2 = find(j);
					if(t1 != t2) {
						p[t1] = t2;
					}
				}
			}
		}
		else {
			scanf("%d%d", &a, &b);
			int t1 = find(a);
			int t2 = find(b);
			if(t1 == t2) printf("SUCCESS\n");
			else printf("FAIL\n");
		}
	}
	return 0;
}


你可能感兴趣的:(POJ 2236)