链接: 题目.
题意:有1到n台坏电脑,每台电脑可连接半径是d,有两个操作:
1、O p,表示修复第p台电脑。
2、S p q,判断p和q两台电脑能不能连接(直接或间接连接)。能连输出SUCCESS,否则输出FAIL
思路:直接连接很好判断,间接连接就判断在不在同一个集合里。
代码:
#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <unordered_set>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define pb push_back
#define T int T;scanf("%d",&T);while(T--)
const ll mod=1e9+7;
pair<int,int> place[1005];
int f[1005];
int n,d;
int vis[1005];
int find(int x){
if(f[x]==x) return x;
return f[x] = find(f[x]);
}
int main(){
scanf("%d %d", &n, &d);
for(int i = 1; i <= n; i ++){
f[i] = i;
scanf("%d %d", &place[i].first, &place[i].second);
}
char c;
while(cin >> c){
if(c=='O'){
int p;
scanf("%d", &p);
if(vis[p]==0){ //没修过才更新并查集。
for(int i = 1; i <= n; i ++){ //间接连接
if(vis[i]&&pow(place[i].first-place[p].first,2)+pow(place[i].second-place[p].second,2)<=d*d){
int fx = find(i);
int fy = find(p);
f[fx] = fy;
}
}
}
vis[p] = 1; //修过
}else{
int p,q;
scanf("%d %d", &p, &q);
if(vis[q]==0||vis[p]==0){
printf("FAIL\n");
continue;
}
int fp = find(p);
int fq = find(q);
if(fp==fq || pow(place[q].first-place[p].first,2)+pow(place[q].second-place[p].second,2)<=d*d) printf("SUCCESS\n"); //直接或间接连接
else printf("FAIL\n");
}
}
return 0;
}
/*
*/