Wireless Network
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 28617 | Accepted: 11842 |
Description
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.
Input
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.
Output
For each Testing operation, print "SUCCESS" if the two computers can communicate, or "FAIL" if not.
Sample Input
4 1 0 1 0 2 0 3 0 4 O 1 O 2 O 4 S 1 4 O 3 S 1 4
Sample Output
FAIL SUCCESS
Source
POJ Monthly,HQM
简单并查集
1 //2017-07-17 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 6 using namespace std; 7 8 const int N = 1010; 9 int n, d; 10 int fa[N], x[N], y[N]; 11 bool isRepaired[N]; 12 13 void init(){ 14 for(int i = 0; i < N; i++){ 15 fa[i] = i; 16 isRepaired[i] = false; 17 } 18 } 19 20 int getfa(int x){ 21 if(fa[x] == x)return x; 22 return fa[x] = getfa(fa[x]); 23 } 24 25 void merge0(int a, int b){ 26 int af = getfa(a); 27 int bf = getfa(b); 28 if(af != bf){ 29 fa[bf] = af; 30 } 31 } 32 33 int main(){ 34 char op[3]; 35 int a, b; 36 while(scanf("%d%d", &n, &d) != EOF){ 37 init(); 38 for(int i = 1; i <= n; i++){ 39 scanf("%d%d", &x[i], &y[i]); 40 } 41 getchar(); 42 while(scanf("%s", op)!=EOF){ 43 if(op[0] == 'O'){ 44 scanf("%d", &a); 45 isRepaired[a] = true; 46 for(int i = 1; i <= n; i++){ 47 if(isRepaired[i] && (x[i]-x[a])*(x[i]-x[a])+(y[i]-y[a])*(y[i]-y[a])<=d*d){ 48 merge0(i, a); 49 } 50 } 51 }else{ 52 scanf("%d%d", &a, &b); 53 int af = getfa(a); 54 int bf = getfa(b); 55 if(af == bf) printf("SUCCESS "); 56 else printf("FAIL "); 57 } 58 } 59 } 60 61 return 0; 62 }