Wireless Network
Time limit:10000ms Memory limit:65536k
Total submissions:20573 accepted:8648
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 a unexpected Attacked, all computers in the network were all broken. The computers are repaired one by one, and the network gradually began to work. Because of the hardware restricts, each computer can only directly communicate with the computers this are not farther tha n d meters from it. But every computer can be regarded as the intermediary of the communication, between, and other two, which is to computers 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 two kinds to operations at every moment, repairing a computer, O R testing if two computers can communicate. Your job is to answer the testing operations.
Input
The contains two integers n and d (1 <= n <= 1001, 0 <= D <= 20000). Here N was the number of computers, which are numbered from 1 to N, and D is the maximum distance two computers can Communi Cate directly. In the next n lines, each contains two integers xi, yi (0 <= xi., Yi <= 10000), which is the coordinate of n computer S. from the (n+1)-th The "End of", there are operations, which are out one by one. Each line contains a 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 would 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
Meaning
Roughly translated, there are n computers, given the coordinates of the n computers, all the initial state of the computer is all power off, computer and computer if you want to directly connected, must be the distance between the maximum range D. Now do a series of operations, output as required.
A total of 2 operations:
1. Turn on the power of a computer.
2. Inquire whether two computers can contact each other (direct, indirect), if you can, then output success, if not, then output fail.
Exercises
This problem is still a question of whether to be in the same connected component, still use and check the set.
Firstly, the coordinate information is processed, and then the distance between the 1~n computers is calculated, and if it is not greater than D, the adjacent table is added to each other.
The data is then processed according to the operation, and if the power is turned on, it is labeled 1 and added to the set of computers that are adjacent to the open computer. If the query operation, then directly verify whether two computers in the same connected components can be.
Main.cpp//And check collection-a-wireless network////Created by Shan on 16/1/18. COPYRIGHT©2016 year Shan.
All rights reserved. 3313ms 4792kb #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h&
Gt #include <math.h> #include <algorithm> #include <vector> #include <queue> #define MAXN 1005 Struc
t nd{int x,y;};
using namespace Std;
vector<int>dis[maxn];
Nd ND[MAXN];
int n,d,num1,num2;
BOOL VIS[MAXN];
int PRE[MAXN];
Double DISTC (Nd a,nd b) {double dist;
DIST=SQRT (Double) ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y)));
return dist;
int find (int num) {int root,now=num,tmp;
while (Pre[now]!=now) Now=pre[now];
Root=now;
Now=num;
while (Pre[now]!=now) {Tmp=pre[now];
Pre[now]=root;
now=tmp;
return root;
} void Join (int a,int b) {int ra=find (a), rb=find (b);
if (RA!=RB) pre[ra]=rb; int main (int argc, const char * argv[]) {scanf ("%d%d", &n,&d);
memset (ND, 0, sizeof (ND));
memset (Vis, 0, sizeof (VIS));
for (int i=1; i<=n; i++) {scanf ("%d%d", &nd[i].x,&nd[i].y);
Pre[i]=i;
for (int i=1; i<=n; i++) for (int j=i+1; j<=n; J + +) {double DIST=DISTC (nd[i], nd[j]);
if (dist<= (double) d) {dis[i].push_back (j);
Dis[j].push_back (i);
} char op;
while (scanf ("%c", &op)!=eof) {if (op== ' O ') {scanf ("%d", &num1);
Vis[num1]=1; for (int i=0; i<dis[num1].size (); i++) {if (vis[dis[num1][i)]) {Join (dis[num1][i)
, NUM1);
}} else if (op== ' S ') {scanf ("%d%d", &num1,&num2);
if (Find (NUM1) ==find (num2)) printf ("success\n"); else printf ("fail\n");
} return 0;
}