[ An Ac a Day ^_^ ] [kuangbin带你飞]专题五 并查集 POJ 2236 Wireless Network
题意:
一次地震震坏了所有网点 现在开始修复它们
有N个点 距离为d的网点可以进行通信
O p 代表p点已经修复
S p q 代表询问p q之间是否能够通信
思路:
基础并查集
每次修复一个点重新刷一边图就行了
1 /* *********************************************** 2 Author :Sun Yuefeng 3 Created Time :2016/11/1 15:40:35 4 File Name :food.cpp 5 ************************************************ */ 6 7 #include<cstdio> 8 #include<iostream> 9 #include<algorithm> 10 #include<cmath> 11 #include<cstring> 12 #include<string> 13 #include<bitset> 14 #include<map> 15 #include<set> 16 #include<stack> 17 #include<vector> 18 #include<queue> 19 #include<list> 20 #define M(a,b) memset(a,b,sizeof(a)) 21 using namespace std; 22 typedef long long ll; 23 const int inf=0x3f3f3f3f; 24 const int maxn=1e3+10; 25 const int mod=1e7+7; 26 int dx[8]= {0,0,1,-1,1,-1,1,-1}; 27 int dy[8]= {1,-1,0,0,-1,1,1,-1}; 28 29 int n,d; 30 int father[maxn]; 31 bool vis[maxn]; 32 33 struct network 34 { 35 int x,y; 36 int dis(network a) 37 { 38 int xx=a.x-x; 39 int yy=a.y-y; 40 return xx*xx+yy*yy; 41 } 42 }net[maxn]; 43 44 bool judge(network a,network b) 45 { 46 if(a.dis(b)>d*d) return false; 47 return true; 48 } 49 50 int find(int x) 51 { 52 if(father[x]!=x) father[x]=find(father[x]); 53 return father[x]; 54 } 55 56 int main() 57 { 58 //freopen("in.txt","r",stdin); 59 //freopen("out.txt","w",stdout); 60 scanf("%d%d",&n,&d); 61 for(int i=0;i<maxn;i++) 62 vis[i]=false,father[i]=i; 63 for(int i=1;i<=n;i++) 64 scanf("%d%d",&net[i].x,&net[i].y); 65 getchar(); 66 char str[2]; 67 int p,q; 68 while(~scanf("%s",str)) 69 { 70 if(str[0]=='O') 71 { 72 scanf("%d",&p); 73 vis[p]=true; 74 for(int i=1;i<=n;i++) 75 { 76 if(i==p||!vis[i]) continue; 77 if(judge(net[p],net[i])) 78 { 79 father[find(p)]=find(i); 80 } 81 } 82 } 83 else 84 { 85 scanf("%d%d",&p,&q); 86 if(find(p)==find(q)) printf("SUCCESS\n"); 87 else printf("FAIL\n"); 88 } 89 } 90 return 0; 91 }