[ An Ac a Day ^_^ ] [kuangbin带你飞]专题五 并查集 POJ 2236 Wireless Network

时间:2023-03-08 18:42:52
[ An Ac a Day ^_^ ] [kuangbin带你飞]专题五 并查集 POJ 2236 Wireless Network

题意:

一次地震震坏了所有网点 现在开始修复它们

有N个点 距离为d的网点可以进行通信

O p   代表p点已经修复

S p q 代表询问p q之间是否能够通信

思路:

基础并查集

每次修复一个点重新刷一边图就行了

 /* ***********************************************
Author :Sun Yuefeng
Created Time :2016/11/1 15:40:35
File Name :food.cpp
************************************************ */ #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<bitset>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<queue>
#include<list>
#define M(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=1e3+;
const int mod=1e7+;
int dx[]= {,,,-,,-,,-};
int dy[]= {,-,,,-,,,-}; int n,d;
int father[maxn];
bool vis[maxn]; struct network
{
int x,y;
int dis(network a)
{
int xx=a.x-x;
int yy=a.y-y;
return xx*xx+yy*yy;
}
}net[maxn]; bool judge(network a,network b)
{
if(a.dis(b)>d*d) return false;
return true;
} int find(int x)
{
if(father[x]!=x) father[x]=find(father[x]);
return father[x];
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d%d",&n,&d);
for(int i=;i<maxn;i++)
vis[i]=false,father[i]=i;
for(int i=;i<=n;i++)
scanf("%d%d",&net[i].x,&net[i].y);
getchar();
char str[];
int p,q;
while(~scanf("%s",str))
{
if(str[]=='O')
{
scanf("%d",&p);
vis[p]=true;
for(int i=;i<=n;i++)
{
if(i==p||!vis[i]) continue;
if(judge(net[p],net[i]))
{
father[find(p)]=find(i);
}
}
}
else
{
scanf("%d%d",&p,&q);
if(find(p)==find(q)) printf("SUCCESS\n");
else printf("FAIL\n");
}
}
return ;
}