Wireless Network
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.
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.
Output
For each Testing operation, print "SUCCESS" if the two computers can communicate, or "FAIL" if not.
Sample Input
4 10 10 20 30 4O 1O 2O 4S 1 4O 3S 1 4
Sample Output
FAILSUCCESS题解:并查集的应用
- /*
- author: Tiny
- data:6.14
- */
- #include "stdio.h"
- #include "math.h"
- #define N 1005
- int is[N][N];
- typedef struct{
- int x;
- int y;
- int state;
- }Point;
- int parent[N];
- int find(int);
- int un(int, int);
- int main(){
- int n;
- double d;
- int i = 0, j = 0, k = 0, a, b;
- double temp = 0;
- char c;
- Point point[N];
- scanf("%d%lf",&n, &d);
- for(i = 1;i < n+1; i++)
- {
- scanf("%d%d",&point[i].x, &point[i].y);
- point[i].state = 0;
- }
- for(i = 0; i < n+1; i++)
- {
- parent[i] = -1;
- }
- //将一个点与其可以到达的点相联系起来
- for(i = 1; i < n+1; i++)
- for(j = 1, k=1, is[i][0] = 0; j < n+1; j++)
- {
- temp = sqrt((point[i].x - point[j].x)*(point[i].x - point[j].x) + (point[i].y - point[j].y)*(point[i].y - point[j].y));
- if(temp <= d)
- {
- is[i][k++] = j;
- is[i][0]++;
- }
- }
- while(scanf("\n%c", &c) != EOF)
- {
- if(c == 'O')
- {
- scanf("%d", &a);
- point[a].state = 1;
- for(i =1 ; i < is[a][0]+1; i++)
- {
- if(point[is[a][i]].state)
- {
- un(a, is[a][i]);
- }
- }
- }
- if(c == 'S')
- {
- scanf("%d%d",&a, &b);
- if(a == b)
- printf("SUCCESS\n");
- else{
- if(find(a) == find(b))
- printf("SUCCESS\n");
- else
- printf("FAIL\n");
- }
- }
- }
- return 0;
- }
- int find(int key)
- {
- int i, temp;
- for(i = key; parent[i] >= 0; i = parent[i]);//找到最终的父节点
- //路径压缩
- while(key != i)
- {
- temp = parent[key];
- parent[key] = i;
- key = temp;
- }
- return i;
- }
- //第一个加入的节点为最终父节点,注意union的加权规则
- int un(int a, int b)
- {
- int m = find(a), temp, n = find(b);
- if(m == n) return 0;
- temp = parent[m] + parent[n];
- parent[m] = n;
- parent[n] = temp;
- return 1;
- }
C++数据结构原理与经典问题求解