An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers are repaired one by one, and the network gradually began to work again. Because of the hardware restricts, each computer can only directly communicate with the computers that are not farther than d meters from it. But every computer can be regarded as the intermediary of the communication between two other computers, that is to say computer A and computer B can communicate if computer A and computer B can communicate directly or there is a computer C that can communicate with both A and B. 
  
 
  
In the process of repairing the network, workers can take two kinds of operations at every moment, repairing a computer, or testing if two computers can communicate. Your job is to answer all the testing operations.
 
 
 
In the process of repairing the network, workers can take two kinds of operations at every moment, repairing a computer, or testing if two computers can communicate. Your job is to answer all the testing operations.
  The first line contains two integers N and d (1 <= N <= 1001, 0 <= d <= 20000). Here N is the number of computers, which are numbered from 1 to N, and D is the maximum distance two computers can communicate directly. In the next N lines, each contains two integers xi, yi (0 <= xi, yi <= 10000), which is the coordinate of N computers. From the (N+1)-th line to the end of input, there are operations, which are carried out one by one. Each line contains an operation in one of following two formats: 
  
1. "O p" (1 <= p <= N), which means repairing computer p.
2. "S p q" (1 <= p, q <= N), which means testing whether computer p and q can communicate.
 
  
The input will not exceed 300000 lines.
 
 
 
1. "O p" (1 <= p <= N), which means repairing computer p.
2. "S p q" (1 <= p, q <= N), which means testing whether computer p and q can communicate.
The input will not exceed 300000 lines.
  For each Testing operation, print "SUCCESS" if the two computers can communicate, or "FAIL" if not.
 
 
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
	题意:一个地方发生了地震,然后有N个电脑,现在要让每电脑是否能联系, 只要A和B直接联系,B和C直接联系,那么A和C就可以联系,但是直接联系只能在指定的距离内进行联系。 
 思路:用结构体记录下每个点的横纵坐标,然后当每次修复一个电脑时候就去遍历一边看是否能联系,下面是代码:#include <iostream> #include <cstdio> #include <cmath> #include <cstring> using namespace std; int fa[10000 + 10]; bool com[10000 + 10]; struct computer { int x, y; }computers[10000 + 10]; int ssqrt(int x, int i) { return (computers[x].x - computers[i].x)*(computers[x].x - computers[i].x) + (computers[x].y - computers[i].y)*(computers[x].y - computers[i].y); } void init() { for(int i = 1; i < 10000 + 10; i++) fa[i] = i; memset(computers, 0, sizeof(computers)); memset(com, 0, sizeof(com)); } int getf(int x) { if(x == fa[x]) return x; return fa[x] = getf(fa[x]); } void Union(int x, int y) { x = getf(x); y = getf(y); if(x == y) return; fa[y] = x; } int main() { int n, d; scanf("%d %d", &n, &d); init(); for(int i = 1; i <= n; i++) scanf("%d %d", &computers[i].x, &computers[i].y); getchar(); char s; while(scanf("\n%c", &s) != EOF) { if(s == 'O') { int x; scanf("%d", &x); for(int i = 1; i <= n; i++) // 遍历是否可以直接联系 { if(ssqrt(x, i) <= d * d && com[i] == 1 && x != i) Union(x, i); } com[x] = 1; } else { int x, y; scanf("%d%d",&x, &y); x = getf(x); y = getf(y);//判断是否可以联系 if(x != y) { printf("FAIL\n"); } else printf("SUCCESS\n"); } } return 0; }
 
                   
                   
                   
                   
                            
 
                             本文介绍了一种在地震灾害后的网络重建方案,通过修复受损的计算机节点并建立无线通讯网络,确保在限定距离内的设备能够直接或间接地相互通信。文中详细描述了如何利用坐标信息和距离限制来实现设备间的通讯连接,并通过具体实例演示了修复过程及测试方法。
本文介绍了一种在地震灾害后的网络重建方案,通过修复受损的计算机节点并建立无线通讯网络,确保在限定距离内的设备能够直接或间接地相互通信。文中详细描述了如何利用坐标信息和距离限制来实现设备间的通讯连接,并通过具体实例演示了修复过程及测试方法。
           
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
              
             
                   242
					242
					
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
            


 
            