题目链接
此题是并查集的简单应用,难度不大。(没花太多时间就看到Accepted的感觉好爽,哈哈哈)
懒得写其他的东西了。
#include <iostream>
#include <cstdio>
#include <cstring>
#define MAX 1010
using namespace std;
int N, D;
struct Coordinate
{
int Xi;
int Yi;
}Co[MAX];
bool Link(int x, int y)
{
if ((Co[x].Xi - Co[y].Xi)*(Co[x].Xi - Co[y].Xi) + (Co[x].Yi - Co[y].Yi)*(Co[x].Yi - Co[y].Yi) <= D * D)
return true;
return false;
}
bool Work[MAX];
int father[MAX];
int Find(int x)
{
int r = x;
while (father[r] != r)
r = father[r];
//压缩路径
int i = x;
int j;
while (father[i] != r)
{
j = father[i];
father[i] = r;
i = j;
}
return r;
}
void Union(int x, int y)
{
int fx = Find(x);
int fy = Find(y);
if (fx != fy)
father[fx] = fy;
}
int main()
{
memset(Work, 0, sizeof(Work));
cin >> N >> D;
for (int i = 0; i<=N; i++)
father[i] = i;
char Op;
int a, b;
int c;
for (int i = 1; i<=N; i++)
{
scanf("%d%d", &Co[i].Xi, &Co[i].Yi);
}
while (cin>>Op)
{
if (Op == 'O')
{
cin >> c;
Work[c] = true;
for (int i = 1; i<=N; i++)
{
if (i != c)
{
if (Work[i] && Link(i, c))
Union(i, c);
}
}
}
else
{
cin >> a >> b;
if (Find(a) == Find(b))
cout << "SUCCESS" << endl;
else
cout << "FAIL" << endl;
}
}
return 0;
}