題意:給你一些電腦,全是壞的。你可以修。他們之間的最長通信距離爲d,可以由中間電腦傳遞信息。
一道簡單的並查集,唯一比較麻煩的是需要遍歷電腦來看它們能否聯通,不過此題給的時間夠大。
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn = 1001 + 5;
int n, d;
struct node
{
int x, y;
};
node corr[maxn];
bool vis[maxn];
int fa[maxn];
double dist(int x, int y)//計算距離記得用浮點數
{
return sqrt((corr[x].x - corr[y].x) * (corr[x].x - corr[y].x) + (corr[x].y - corr[y].y) * (corr[x].y - corr[y].y));
}
void init()
{
for(int i = 0; i <= n + 1; i++)
fa[i] = i;
memset(vis, 0, sizeof(vis));
}
int root(int x)
{
if(x != fa[x])
fa[x] = root(fa[x]);
return fa[x];
}
bool same(int x, int y)
{
return root(x) == root(y);
}
void unite(int x, int y)
{
int fx = root(x);
int fy = root(y);
fa[fx] = fy;
}
int main()
{
while(scanf("%d%d", &n, &d) == 2)
{
init();
for(int i = 1; i <= n; i++)
scanf("%d%d", &corr[i].x, &corr[i].y);
char cmd[10];
while(scanf("%s", cmd) == 1)
{
if(cmd[0] == 'O')
{
int t;
scanf("%d", &t);
vis[t] = 1;
for(int i = 1; i <= n; i++)//遍歷查看是否有新的接通
{
//printf("yes\n");
if(i != t && vis[i] && !same(i, t) && dist(i, t) <= d)
{
unite(i, t);
}
}
}
else if(cmd[0] == 'S')
{
bool flag = false;
int t1, t2;
scanf("%d%d", &t1, &t2);
if(vis[t1] && vis[t2])
{
if(same(t1, t2)) flag = true;
}
if(flag) printf("SUCCESS\n");
else printf("FAIL\n");
}
}
}
return 0;
}