码迷,mamicode.com
首页 > Web开发 > 详细

POJ 2236 Wireless Network

时间:2015-10-18 15:28:38      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

预处理一下每个点的相邻点,直接并差集。

路径压缩加了按秩合并的优化效果不明显(甚至还有点常数),大概是路径压缩的上界很松吧。

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
//#include<bits/stdc++.h>
using namespace std;

#define PB push_back
const int maxn = 1e3+2;
double x[maxn], y[maxn];
bool vis[maxn];
vector<int> nei[maxn];
inline double squ(double a){ return a*a; }
inline double dist(int i,int j){ return sqrt(squ(x[i]-x[j])+squ(y[i]-y[j])); }
const int eps = 1e-9;
int pa[maxn], hei[maxn];
int fdst(int x){ return x==pa[x]?x:pa[x]=fdst(pa[x]); }
inline void mrge(int a,int b)
{
    int s1 = fdst(a), s2 = fdst(b);
    if(s1 != s2){
        if(hei[s1] < hei[s2]) pa[s1] = s2;
        else {
            pa[s2] = s1;
            if(hei[s1] == hei[s2]) hei[s1]++;
        }
    }
}

//#define LOCAL
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    int N, d; scanf("%d%d",&N,&d);
    for(int i = 1; i <= N; i++){
        scanf("%lf%lf",x+i,y+i);
        pa[i] = i;
    }
    for(int i = 1; i <= N; i++){
        for(int j = 1; j < i; j++){
            if(dist(i,j)<= d){
                nei[i].PB(j);
                nei[j].PB(i);
            }
        }
    }
    char op[2];
    int p,q;
    while(~scanf("%s",op)){
        scanf("%d",&p);
        if(*op==O){
            if(vis[p]) continue;
            vis[p] = true;
            for(int i = 0; i < (int)nei[p].size(); i++){
                if(vis[nei[p][i]])
                    mrge(p,nei[p][i]);
            }
        }else {
            scanf("%d",&q);
            puts(fdst(p) == fdst(q)?"SUCCESS":"FAIL");
        }
    }
    return 0;
}

 

POJ 2236 Wireless Network

标签:

原文地址:http://www.cnblogs.com/jerryRey/p/4889452.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!