POJ2236 wireless network 【并查集水题】
2014-05-20 20:21
295 查看
一张图上分布着n台坏了的电脑,并知道它们的坐标。两台修好的电脑如果距离<=d就可以联网,也可以通过其他修好的电脑间接相连。给出操作“O x”表示修好x,给出操作“S x y”,请你判断x和y在此时有没有连接上。
#include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #include <cstring> #include <cmath> const int MAXN=111111; const int MAX_N=111111; int n,d; using namespace std; struct po { int x,y; bool on; }point[MAXN]; bool disok(int a,int b) { int dx=point[a].x-point[b].x; int dy=point[a].y-point[b].y; if(dx*dx+dy*dy<=d*d) return true; else return false; } //并查集 int par[MAX_N]; int rank[MAX_N];//树的高度 void init(int n) { for(int i=0;i<n;i++) { par[i]=i; rank[i]=0; } } int find(int x)// { if(par[x]==x) return x; else return par[x]=find(par[x]); } //合并x和y所属的集合 void unite(int x,int y) { x=find(x); y=find(y); if(x==y) return; if(rank[x]<rank[y]) par[x]=y; else { par[y]=x; if(rank[x]==rank[y]) rank[x]++; } } //判断x和y是否同属于一个集合 bool same(int x, int y) { return find(x)==find(y); } int main() { #ifndef ONLINE_JUDGE freopen("G:/1.txt","r",stdin); freopen("G:/2.txt","w",stdout); #endif scanf("%d%d",&n,&d); init(n); for(int i=1;i<=n;i++) scanf("%d%d",&point[i].x,&point[i].y); char kind[2]; while(scanf("%s",kind)!=EOF) { if(strcmp(kind,"O")==0) { int nn; scanf("%d",&nn); point[nn].on=true; for(int i=1;i<=n;i++) { if(point[i].on&&disok(i,nn)) unite(nn,i); } } else { int s1,s2; scanf("%d%d",&s1,&s2); if(same(s1,s2)) { cout<<"SUCCESS"<<'\n'; } else { cout<<"FAIL"<<'\n'; } } } }
相关文章推荐
- 基础并查集poj2236
- poj 2524宗教信仰 并查集水题
- How Many Tables 并查集+水题+hdu
- 修复公路 洛谷1111 并查集 水题
- poj2236 Wireless Network(并查集)
- L2-010. 排座位(并查集水题)
- hdu1272(并查集水题,然而我状态处于低谷,无限wa呜呜呜)
- HDU 3172 并查集水题
- ABBYY Cup 2.0 - Easy, problem: (C1) Party 并查集水题
- 修复公路 洛谷1111 并查集 水题
- POJ1611 The Suspects ACM解题报告(并查集水题)
- POJ2236(并查集)
- poj2236 并查集
- NOJ 1798 并查集水题
- HDU - 1272 小希的迷宫(看似并查集的水题)
- 修复公路 洛谷1111 并查集 水题
- 【并查集小结】今日并查集水题
- HDU1232 畅通工程 并查集(第一题) 水题
- poj2236 并查集
- 并查集水题