POJ2236 Wireless Network

时间:2021-06-17 08:35:32

解题思路:简单并查集,注意时间限制是10000MS,每次进行O操作之后,

      进行一次for循环,进行相关调整。同时注意输入输出格式,见代码:

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = ;
int father[maxn], vis[maxn]; struct node{
double x, y; //此处用double
}p[maxn]; int Find(int x)
{
return father[x] == x ? x : father[x] = Find(father[x]);
} void Union(int x, int y)
{
int rootx = Find(x), rooty = Find(y);
if(rootx != rooty) father[rootx] = rooty;
return ;
} int main()
{
int n, d, k, a, b;
char ch;
scanf("%d %d", &n, &d);
memset(vis, , sizeof(vis));
for(int i = ; i <= n; i++) father[i] = i;
for(int i = ; i <= n; i++) scanf("%lf %lf", &p[i].x, &p[i].y);
while(~scanf(" %c", &ch))
{
if(ch == 'O')
{
scanf("%d", &k);
vis[k] = ;
for(int i = ; i <= n; i++)
{
if(!vis[i]) continue; //必须是已经修过的
//两点距离小于等于d,则可以合并
if(sqrt((p[i].x-p[k].x)*(p[i].x-p[k].x)+(p[i].y-p[k].y)*(p[i].y-p[k].y)) <= d)
Union(i, k);
}
}
else
{
scanf("%d %d", &a, &b);
//根节点相同,则表明可以连接
if(Find(a) == Find(b)) printf("SUCCESS\n");
else printf("FAIL\n");
}
}
return ;
}