Wireless Network POJ - 2236
作者:互联网
考察:并查集基础
思路:
只有维修后的电脑才是有用的,因此我们用数组来标记维修后的电脑,当它们能连通需要以下条件:
- x电脑和y电脑都是维修后的
- x电脑与y电脑距离的平方小于d*d
- 如果距离大于d*d,那么它们之间存在中介点,且中介点也被维修过
1 #include <iostream> 2 #include <algorithm> 3 #include <vector> 4 using namespace std; 5 const int N = 1010; 6 int p[N],n,d,vis[N]; 7 vector<int> rep; 8 struct Node{ 9 int x,y; 10 }Nodes[N]; 11 int Getdis(int i,int j) 12 { 13 return (Nodes[i].x-Nodes[j].x)*(Nodes[i].x-Nodes[j].x)+(Nodes[i].y-Nodes[j].y)*(Nodes[i].y-Nodes[j].y); 14 } 15 int findf(int x) 16 { 17 if(x!=p[x]) p[x] = findf(p[x]); 18 return p[x]; 19 } 20 void check(int x) 21 { 22 for(int i=0;i<rep.size();i++){ 23 if(rep[i]!=x&&Getdis(rep[i],x)<=d*d) p[findf(x)] = findf(rep[i]); 24 // printf("%d与%d的距离是%d\n",x,rep[i],Getdis(rep[i],x)); 25 } 26 } 27 int main() 28 { 29 // freopen("in.txt","r",stdin); 30 char op[2]; 31 scanf("%d%d",&n,&d); 32 for(int i=1;i<=n;i++) { scanf("%d%d",&Nodes[i].x,&Nodes[i].y); } 33 while(scanf("%s",op)!=EOF) 34 { 35 if(op[0]=='O'){ 36 int x; scanf("%d",&x); 37 vis[x] = 1; 38 p[x] = x; 39 check(x); rep.push_back(x); 40 }else if(op[0]=='S'){ 41 int x,y; scanf("%d%d",&x,&y); 42 if(findf(x)==findf(y)&&vis[x]&&vis[y])printf("SUCCESS\n"); 43 else printf("FAIL\n"); 44 } 45 } 46 return 0; 47 }
标签:维修,return,Network,int,电脑,Wireless,POJ,Nodes,include 来源: https://www.cnblogs.com/newblg/p/14224797.html