#include<iostream> #include<algorithm> #include<vector> using namespace std; int coor[10005][10005]; int par[1005]; int rank[1005]; void init(int n){ for(int i=1;i<=n;i++){ par[i]=i; rank[i]=0; } } int find(int d){ if(par[d]==d) return d; else return par[d]=find(par[d]); } void unite(int a,int b){ int x=find(a); int y=find(b); if(x==y)return; if(rank[x]<rank[y]) par[x]=y; else{ par[y]=x; if(rank[x]==rank[y])rank[x]++; } } bool same(int x,int y){ return find(x)==find(y); } int main(){ int n,d; cin>>n>>d; d=d*d; init(n); vector<pair<int,int> > coordinate(n+1); for(int i=1;i<=n;i++){ int x,y; cin>>x>>y; coordinate[i]=(make_pair(x,y)); } char c; vector<int> repaired; while(cin>>c){ if(c=='S'){ int a,b; cin>>a>>b; if(same(a,b)) cout<<"SUCCESS"<<endl; else cout<<"FAIL"<<endl; } else{ int a; cin>>a; int x=coordinate[a].first; int y=coordinate[a].second; repaired.push_back(a); for(int i=0;i<repaired.size();i++){ if(repaired[i]!=a){ int xi=coordinate[repaired[i]].first; int yi=coordinate[repaired[i]].second; if((x-xi)*(x-xi)+(y-yi)*(y-yi)<=d){ unite(repaired[i],a); } } } } } return 0; }
http://poj.org/problem?id=2236