http://poj.org/problem?id=2236
题意:现有一些电脑(编号从 1 - N),在修理好某台电脑并且当这台电脑与其他电脑距离不超过 D 的情况下, 其他电脑可以由这台电脑控制。
有两种操作:一是修理编号为 x 的某台电脑, 而是询问你编号从 p - q 的电脑是否都彼此联通
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <cstdlib> #include <cstring> using namespace std; #define maxn 1500 #define INF 0x3f3f3f3f int a[maxn], b[maxn], father[maxn], v[maxn]; int n, d; int Find(int x) { while(x != father[x]) { x = father[x]; } return x; } int ok(int i, int j) { int s = (a[i]-a[j])*(a[i]-a[j])+(b[i]-b[j])*(b[i]-b[j]); if( s <= d) return 1; return 0; } int main() { scanf("%d %d", &n, &d); d *= d; for(int i=1; i<=n; i++) scanf("%d %d", &a[i], &b[i]); for(int i=1; i<=n; i++) father[i] = i; char op[5]; int x, y; memset(v, 0, sizeof(v)); while(scanf("%s", op) != EOF) { if(op[0]=='S') { scanf("%d %d", &x, &y); if(Find(x) == Find(y)) printf("SUCCESS "); else printf("FAIL "); continue; } scanf("%d", &x); if(!v[x]) { v[x] = 1; for(int i=1; i<=n; i++) { if(v[i] && x != i && ok(i, x)) { int p = Find(i); int q = Find(x); if(p!=q) father[q]= p; } } } } return 0; }