Wireless Network
Time Limit: 10000MS |
|
Memory Limit: 65536K |
Total submissions: 20537 |
|
accepted: 8631 |
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 of Operations at every moment, repairing a computer, or testing if two computers can communicate. Your job is to answer all testing operations.
Input the "the" I line 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 2 o 4
s 1 4
o 3
s 1 4
Sample Output
FAIL
SUCCESS
There are n computers in an area, and the coordinates of these computers are xi,yi. Because the cable is damaged, the two computers within the distance of D can communicate, if the distance is more than D, but medium computer can also keep communication. At first, these computers are bad, O p means to repair the P Taiwan computer, S Q p Indicates whether Q and P computers can communicate, is the output success output fall.
The problem is obvious and the only thing to notice is that the computer is repaired and the distance between the computers is less than D to merge.
The code is as follows:
#include <cstdio> #include <cstring> #include <cmath> int tree[1010];
int sign[1010];//mark repaired computer//double map[1010][1010]; int mark[1010][1010];//marks two computer struct node {int x,y with distance range within D;
CPU[1010];
int n,d;
int find (int r) {if (tree[r]==r) return R;
else return Tree[r]=find (Tree[r]);
} void Merge (int a,int b) {int fa=find (a);
int Fb=find (b);
if (FA!=FB) TREE[FA]=FB;
} void Setmap ()//to point to map {int i,j; for (I=1;i<=n;++i) {for (j=1;j<=n;++j) {double dis=sqrt (cpu[i].x-cpu[j].x) * (cpu[i].x-cpu[j].x) *1.0+ (cpu[i).
Y-CPU[J].Y) * (CPU[I].Y-CPU[J].Y) *1.0);
if (dis<d+0.00000001)//Pay attention to repair accuracy Ah, will WA's mark[i][j]=mark[j][i]=1;
int main () {int i,a,b;
Char s[5];
while (scanf ("%d%d", &n,&d)!=eof) {for (i=1;i<=n;++i) scanf ("%d%d", &cpu[i].x,&cpu[i].y);
for (i=1;i<=n;++i) tree[i]=i;
memset (Mark,0,sizeof (Mark));
memset (sign,0,sizeof (sign));
Setmap (); while (scanf ("%s", s)!=eof) {if (s[0]== ' O ') {scanF ("%d", &a);
Sign[a]=1;
for (I=1;i<=n;++i) {if (Mark[i][a]&&sign[i]) merge (a,i);
} else {scanf ("%d%d", &a,&b);
if (Find (a) ==find (b)) printf ("success\n");
else printf ("fail\n");
}} return 0; }