Test instructions
There are n computers that are damaged, and they are now going to be repaired on a per-station basis, and they will resume communication functions with each other. If two computers can communicate with each other, there are two cases, one is the distance between them is less than D, and the second is that they can access the third repaired computer. Give all the coordinates of the computer, the two possible operations, O X means to repair the X, S x y to determine whether the communication between x y, if you can output success, otherwise output fall.
Ideas:
Use and check sets to keep the computer connected to each other.
Each time the computer is repaired, it communicates with the computer (the distance is satisfied and repaired) to connect with it.
#include <cstdio> #include <cstring> #include <string> #include <iostream> using namespace s td;const int MAXN = + 5;struct node{int x, y;} Pos[maxn];int P[MAXN], vis[maxn];int find (int x) {return p[x] = = x = x:p[x] = find (P[x]);} void Unionset (int x, int y) {int px = find (x), py = Find (y), if (px! = py) p[px] = py;} int main () {int n, d;scanf ("%d%d", &n, &d), memset (Vis, 0, sizeof (VIS)), for (int i = 1; I <= n; ++i) P[i] = I;fo R (int i = 1; I <= n; ++i) scanf ("%d%d", &pos[i].x, &pos[i].y); GetChar (); Char S[20];while (fgets (S, sizeof (s), stdin) = NULL) {Char op;int A, b;if (s[0] = = ' s ') {sscanf (S, "%c%d%d", &op, &a, &b); int rx = Find (a), Ry = Find (b), if (Rx = = ry) printf ("success\n"), Else printf ("fail\n");} else {sscanf (S, "%c%d", &op, &a); Vis[a] = 1;int x = pos[a].x, y = pos[a].y;for (int i = 1; I <= n; ++i) {if (a = = I | | !vis[i]) Continue;int x1 = pos[i].x, y1 = pos[i].y;if ((x-x1) * (x-x1) + (y-y1) * (y-y1) <= d*d) {Unionset (A, I);}}} return 0;}
POJ 2236 Wireless Network (and collection)