标签:RoCE cal proc following ack 台电脑 att rsh with
4 1 0 1 0 2 0 3 0 4 O 1 O 2 O 4 S 1 4 O 3 S 1 4
FAIL SUCCESS
并查集,维修每台电脑时,暴力遍历查找与该电脑距离小于d的已维修电脑,将两者Union。查询时只需查看两台电脑是否在同一并查集中即可。
1 #include <cstdio> 2 #include <iostream> 3 #include <cmath> 4 #include <cstdlib> 5 #include <cstring> 6 using namespace std; 7 int n,d; 8 int x[1005],y[1005],vis[1005]; 9 int fa[1005]; 10 double dis(int xa,int ya,int xb,int yb){ 11 return sqrt((xa-xb)*(xa-xb) + (ya-yb)*(ya-yb)); 12 } 13 14 int getfa(int x){ 15 return ((x == fa[x]) ? x : fa[x] = getfa(fa[x])); 16 } 17 18 void Union(int x,int y){ 19 int tx = getfa(x); 20 int ty = getfa(y); 21 fa[tx] = ty; 22 } 23 24 int main(){ 25 scanf("%d%d",&n,&d); 26 for (int i = 1;i <= n;++i){ 27 scanf("%d%d\n",x+i,y+i); 28 fa[i] = i; 29 } 30 memset(vis,0,sizeof(vis)); 31 char q; 32 int tx,ty; 33 while(~scanf("%c %d",&q,&tx)){ 34 if (q == ‘O‘){ 35 vis[tx] = 1; 36 for (int i = 1;i <= n;++i){ 37 if (i == tx || !vis[i]) continue; 38 double dist = dis(x[i],y[i],x[tx],y[tx]); 39 if (dist < d || fabs(dist - d) < 1e-12) 40 Union(i,tx); 41 } 42 }else{ 43 scanf("%d",&ty); 44 if (getfa(tx) == getfa(ty)){ 45 puts("SUCCESS"); 46 }else puts("FAIL"); 47 48 } 49 getchar(); 50 } 51 return 0; 52 }
标签:RoCE cal proc following ack 台电脑 att rsh with
原文地址:https://www.cnblogs.com/mizersy/p/12233316.html