poj2236 Wireless Network

两点距离,满足小于d就可以连接。其他并查集模板题。

#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <ctime>
#include <assert.h>

#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define eps 1e-8
#define M_PI 3.141592653589793

typedef long long ll;
const ll mod=1000000007;
const int inf=0x7fffffff;
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
using namespace std;
struct node
{
    int f,x,y;
}a[1110];
int n,d,v[1110];
int Find(int x)
{
    return a[x].f==x ? x : Find(a[x].f);
}

void Merge(node x,node y)
{
   int tx=Find(x.f);
   int ty=Find(y.f);
   if(tx!=ty){
      if((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y)<=d*d)
         a[tx].f=ty;
   }
}


int main()
{
    memset(v,0,sizeof(v));
    scanf("%d %d",&n,&d);
    for(int i=1;i<=n;i++){
        scanf("%d %d",&a[i].x,&a[i].y);
        a[i].f=i;
    }
    char c;
    while(scanf("\n%c",&c) != EOF){
        if(c=='O'){
           int x;scanf("%d",&x);
           v[x]=1;
           for(int i=1;i<=n;i++){
            if(v[i]&&i!=x) Merge(a[i],a[x]);
           }
        }
        else{
            int a,b;scanf("%d %d",&a,&b);
            if(Find(a)==Find(b)) printf("SUCCESS\n");
            else printf("FAIL\n");
        }
    }
}

你可能感兴趣的:(poj2236 Wireless Network)