Wireless Network
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 43199 | Accepted: 17800 |
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 #include <cstdio> 2 #include <cmath> 3 using namespace std; 4 5 const int maxn = 1000 + 5; 6 struct coordinate { 7 int x, y; 8 } G[maxn]; 9 int n, d, p, q, cnt, head[maxn], Rank[maxn]; 10 int isrepair[maxn]; 11 char operation; 12 13 double dis(int u, int v) { 14 return sqrt((double)(G[u].x - G[v].x) * (G[u].x - G[v].x) + (G[u].y - G[v].y) * (G[u].y - G[v].y)); 15 } 16 17 int find(int x) { 18 if(head[x] == x) return x; 19 return head[x] = find(head[x]); 20 } 21 22 void Union_set(int x, int y) { 23 int dx = find(x), dy = find(y); 24 if(dx == dy) return; 25 if(Rank[dx] > Rank[dy]) head[dx] = dy; 26 else { 27 head[dy] = dx; 28 if(Rank[dx] == Rank[dy]) 29 Rank[dx] ++; 30 } 31 } 32 33 bool Is_same(int u, int v) { 34 return find(u) == find(v); 35 } 36 37 int main () { 38 cnt = 1; 39 scanf("%d %d", &n, &d); 40 for(int i = 1; i <= n; i ++) 41 scanf("%d %d", &G[i].x, &G[i].y); 42 for(int i = 1; i <= n; i ++) { 43 head[i] = i; 44 Rank[i] = 0; 45 } 46 while(~scanf("%c", &operation)) { 47 if(operation == 'O') { 48 scanf("%d", &p); 49 for(int i = 1; i <= cnt; i ++) { 50 if(dis(p, isrepair[i]) <= d) { 51 Union_set(isrepair[i], p); 52 } 53 } 54 isrepair[cnt ++] = p; 55 } 56 else if(operation == 'S') { 57 scanf("%d %d", &p, &q); 58 if(Is_same(p, q)) 59 printf("SUCCESS "); 60 else 61 printf("FAIL "); 62 } 63 } 64 return 0; 65 }