Wireless Network
Time Limit: 10000MS |
|
Memory Limit: 65536K |
Total Submissions: 16065 |
|
Accepted: 6778 |
Description
An earthquake takes place in southeast Asia. The ACM (Asia cooperated Medical team) has set up a wireless network with the lap computers, but an unexpected aftershock Attacked, all computers on the network were all broken. The computers is 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 is not farther tha n d meters from it. But every computer can be regarded as the intermediary of the communication between and the other computers, that's to say Co Mputer A and Computer B can communicate if computer A and computer B can communicate directly or there is A computer C tha T can communicate with both A and B.
In the process of repairing the network, workers can take the other kinds of operations at every moment, repairing a computer, O R Testing if computers can communicate. Your job is to answer all the testing operations.
Input
The first line contains the integers n and d (1 <= n <= 1001, 0 <= D <= 20000). Here N is the number of computers, which was numbered from 1 to N, and D is the maximum distance the computers can Communi Cate directly. In the next N lines, each contains. Integers xi, yi (0 <= xi, Yi <= 10000), which is the coordinate of N computer S. from the (n+1)-th line to the end of input, there is operations, which is carried out one by one. Each line contains a operation in one of following and 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 would not be exceed 300000 lines.
Output
For each testing operation, print "SUCCESS" if the both computers can communicate, or "FAIL" if not.
Sample Input
4 4O 1O 2O 4S 1 4O 3S 1 4
Sample Output
Failsuccess
Source
POJ MONTHLY,HQM
and check set, improve the problem . and the application of the collection, turned a little bend.
Test Instructions : There is a computer network all the lines are broken, the network has n computers, now you can do two operations, repair (O) and detect whether two computers connected (S), only repaired computer can connect. There is a rule that the distance between two computers cannot exceed a given maximum distance of D (the coordinates of the computer will be given to you at the beginning). Detects when the output two computers can be connected.
ideas : Every time you repair a computer, walk through all the repaired computer, see if the distance is <=d, if the matching instructions can be connected, the collection of two computers is merged. determine whether the two computers are in the same collection each time you check.
Note :
1, the computer numbers given after the coordinates are n+1. For example, o 3, he actually repaired the computer numbered 2, because the computer was numbered starting from 0. 2. When comparing distance, pay attention to use floating-point number to compare, otherwise will WA. 3, "FAIL" Do not write "FALL". 4, the string input when the attention to handle good returns, spaces and so on. 5, note the range of n (1 <= n <= 1001), the maximum is 1001, not 1000. is a small hole, the array is small may be wrong oh. also test the efficiency of using path compression, and not using path compression. The use is 1079MS (c + +), which is 2032MS (c + +), and the time is shortened by one times. in addition, C + + and g++ commit time difference is also very large. The same Code g++ becomes 3063MS (g++).
code :
1#include <iostream>2#include <stdio.h>3#include <cmath>4 using namespacestd;5 6 #defineMAXN 10107 8 intDX[MAXN],DY[MAXN];//coordinates9 intPAR[MAXN];//Par[x] Represents the parent node of xTen intREPAIR[MAXN] ={0}; One intN; A - voidInit ()//Initialize - { the inti; - for(i=0; i<=n;i++) -Par[i] =i; - } + - intFind (intX//querying the root node of x and path compression + { A if(par[x]!=x) atPAR[X] =Find (par[x]); - returnPar[x]; - } - - voidUnion (intXintY//merging x and y collections - { inPar[find (x)] =Find (y); - } to + intAbs (intN) - { the returnN>0? n:-N; * } $ Panax Notoginseng DoubleDis (intAintb) - { the returnsqrtDouble(Dx[a]-dx[b]) * (Dx[a]-dx[b]) + (Dy[a]-dy[b]) * (dy[a]-dy[b])); + } A the intMain () + { - intd,i; $ $ //Initialize -scanf"%d%d",&n,&d); - Init (); the - //Input coordinatesWuyi for(i=0; i<n;i++){ thescanf"%d%d",&dx[i],&dy[i]); - } Wu - //Operation About Charcmd[2]; $ intp,q,len=0; - while(SCANF ("%s", cmd)! =EOF) { - Switch(cmd[0]){ - Case 'O': Ascanf"%d",&p); +p--; therepair[len++] =p; - for(i=0; i<len-1; i++)//Traverse all the repaired computers to see if they can connect $ if(Repair[i]!=p && Dis (repair[i],p) <=Double(d)) the Union (repair[i],p); the Break; the Case 'S': thescanf"%d%d",&p,&q); -P--, q--; in if(Find (P) ==find (q))//whether there is a way theprintf"success\n"); the Else Aboutprintf"fail\n"); the default: the Break; the } + } - the return 0;Bayi}
freecode:www.cnblogs.com/yym2013