标签:
题意:有n个电脑坏了,编号从1到n,现在要修电脑且使电脑间能够通信,如果两台修好的电脑之间直接距离小于等于d就可以通信,如果电脑a和电脑c的距离大于d但a和c都可以与电脑b通信,那么a和c就可以通信了。O a,修好电脑a。S a b询问a和b是否能通信。
题解:每修好一台电脑,就过一遍所有电脑,把修好且能通信的电脑放到一个集合里,询问时只要判断是否在集合内就行了。
#include <stdio.h>
#include <math.h>
const int N = 1005;
struct P {
int x, y;
}p[N];
int n, pa[N], vis[N], d;
char str[5];
int Count(P a, P b) {
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
int get_parent(int x) {
return x == pa[x] ? x : pa[x] = get_parent(pa[x]);
}
int main() {
scanf("%d%d", &n, &d);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &p[i].x, &p[i].y);
pa[i] = i;
}
while (scanf("%s", str) != EOF) {
if (str[0] == ‘O‘) {
int a;
scanf("%d", &a);
vis[a] = 1;
for (int i = 1; i <= n; i++)
if (i != a && vis[i] && Count(p[a], p[i]) <= d * d) {
int px = get_parent(a);
int py = get_parent(i);
if (px != py) {
pa[px] = py;
}
}
}
else {
int a, b;
scanf("%d%d", &a, &b);
if (get_parent(a) == get_parent(b))
printf("SUCCESS\n");
else
printf("FAIL\n");
}
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/hyczms/article/details/45440387