Wireless Network
Time Limit: 10000MS |
|
Memory Limit: 65536K |
Total Submissions: 16832 |
|
Accepted: 7068 |
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
Be careful to distinguish between what has been repaired and what has not.
Code:
#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h>using namespace STD; #define M 1005#define LL __int64struct node{int x, y;} S[m];int Map[m][m], fat[m], vis[m];int f (int x) {if (fat[x]! = x) fat[x] = f (fat[x]); return fat[x];} int main () {int n, d;scanf ("%d%d", &n, &d), int i, j;d*=d;for (i = 1; I <= n; i + +) {scanf ("%d%d", &s[i].x, &am P;S[I].Y); Map[i][i] = 0;for (j = i-1; j> 0; J-) {Map[j][i] = map[i][j] = (s[i].x-s[j].x) * (s[i].x-s[j].x) + (s[i].y-s[j].y ) * (S[I].Y-S[J].Y);//printf ("%d.. Map (%d,%d) \ n ", Map[i][j], I, j);}} Char Ss[2];int A, b;memset (vis, 0, sizeof (VIS)), for (i = 1; I <= n; i++) Fat[i] = I;while (scanf ("%s", ss) = = 1) {if (Ss[0] = = ' O ') {scanf ("%d", &a); Vis[a] = 1;for (i = 1; I <= n; i + +) {if (Vis[i]&&map[a][i] <= d&&i! = a) { int x = f (a); int y = f (i), if (x! = y) fat[x] = y;}} ELSE{SCANF ("%d%d", &a, &b); int x = f (a); int y = f (b); if (vis[a]&&vis[b]&&x = = y) printf ("success\n "); else printf ("fail\n");}} return 0;}
POJ 2236 Wireless Network "and check Set"