/*
translation:
多个电脑连成一个网络,给出修复和查询两种操作。输出每次查询两台电脑是否能够通信的结果
solution:
并查集简单应用即可
note:
注意输入。
date:
2016.10.17
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
const int maxn = 1000 + 5;
struct Point
{
int x, y;
Point(int x_, int y_):x(x_),y(y_){}
Point(){}
} p[maxn];
int par[maxn], high[maxn];
int n, d;
bool isGood[maxn];
vector<int> G[maxn];
void init()
{
for(int i = 0; i <= n; i++) G[i].clear();
memset(isGood, 0, sizeof(isGood));
for(int i = 1; i <= n; i++)
{
par[i] = i;
high[i] = 0;
}
}
int getRoot(int x)
{
return par[x] == x ? x : par[x] = getRoot(par[x]);
}
void unite(int x, int y)
{
x = getRoot(x);
y = getRoot(y);
if(x == y) return;
if(high[x] < high[y]) par[x] = y;
else
{
par[y] = x;
if(high[x] == high[y]) high[x]++;
}
}
bool same(int x, int y)
{
return getRoot(x) == getRoot(y);
}
int getDist(int a, int b)
{
return (p[a].x-p[b].x)*(p[a].x-p[b].x) + (p[a].y-p[b].y)*(p[a].y-p[b].y);
}
int main()
{
//freopen("in.txt", "r", stdin);
while(~scanf("%d%d", &n, &d))
{
init();
for(int i = 1; i <= n; i++) scanf("%d%d", &p[i].x, &p[i].y);
char op;
int p, q;
while(cin >> op)
{
if(op == 'O')
{
cin >> p;
isGood[p] = true;
for(int i = 1; i <= n; i++)
if(isGood[i] && i != p && getDist(p, i) <= d*d)
unite(p, i);
}
else
{
cin >> p >> q;
if(same(p, q)) cout << "SUCCESS" << endl;
else cout << "FAIL" << endl;
}
}
}
return 0;
}
poj2236(并查集)
最新推荐文章于 2019-03-10 15:56:47 发布