Wireless Network POJ 2236(简单并查集)
原题
题目分析
比较明显的并查集题目,先用一个标记数组表示电脑的修复状态,每当修复一个电脑,就用标记数组标记一下,然后遍历一下所有电脑,如果遍历的电脑已被修复且在该电脑连接距离范围内,就合并这两个电脑.查询的话就是常规的并查集查询操作.
代码
1 #include <iostream> 2 #include <algorithm> 3 #include <utility> 4 #include <cstdio> 5 #include <cmath> 6 #include <cstring> 7 #include <string> 8 #include <vector> 9 #include <stack> 10 #include <queue> 11 #include <map> 12 #include <set> 13 14 using namespace std; 15 typedef long long LL; 16 const int INF_INT=0x3f3f3f3f; 17 const LL INF_LL=0x3f3f3f3f3f3f3f3f; 18 19 typedef pair<int,int> P; 20 int n,d; 21 bool c_use[2000]; 22 int com[2000]; 23 int h[2000]; 24 P loca[2000]; 25 26 int ask(int x) 27 { 28 if(com[x]==x) return x; 29 return com[x]=ask(com[x]); 30 } 31 32 void unite(int x,int y) 33 { 34 x=ask(x),y=ask(y); 35 if(x==y) return ; 36 if(h[x]<h[y]) com[x]=y; 37 else 38 { 39 com[y]=x; 40 if(h[y]==h[x]) h[x]++; 41 } 42 } 43 44 int main() 45 { 46 // freopen("black.in","r",stdin); 47 // freopen("black.out","w",stdout); 48 cin>>n>>d; 49 for(int i=1;i<=n;i++) cin>>loca[i].first>>loca[i].second; 50 for(int i=1;i<=n;i++) com[i]=i; 51 getchar(); 52 char x; 53 while(~scanf("%c",&x)) 54 { 55 if(x=='O') 56 { 57 int y; 58 scanf("%d",&y); 59 c_use[y]=true; 60 for(int i=1;i<=n;i++) 61 if(c_use[i]&&(loca[i].first-loca[y].first)*(loca[i].first-loca[y].first)+(loca[i].second-loca[y].second)*(loca[i].second-loca[y].second)<=d*d) unite(i,y); 62 } 63 else 64 { 65 int y,z; 66 scanf("%d %d",&y,&z); 67 if(ask(y)==ask(z)) printf("SUCCESS\n"); 68 else printf("FAIL\n"); 69 } 70 getchar(); 71 } 72 return 0; 73 }