POJ2236 Wireless Network is used for the first time to check the collection, and the first time to write a blog, poj2236wireless
Determine whether two computers can communicate with each other. The two repaired computers can communicate directly within a specified distance, and the two repaired computers can communicate indirectly through one repaired computer. The Code is as follows:
# Include <iostream> # include <cstring> # include <cstdio> using namespace std; int father [1005]; struct Coord // computer coordinate {int x; int y ;} coo [1005]; struct Repair // coordinates and numbers of repaired computers {int x; int y; int numb;} rep [1005]; int Find_fath (int x) // query the set to find the father {if (x! = Father [x]) father [x] = Find_fath (father [x]); // compression return path return father [x];} void Union (int x, int y) // check the parent merge {x = Find_fath (x); y = Find_fath (y); if (x! = Y) father [y] = x;} int main () {// freopen ("in.txt", "r", stdin); int n, d; int num_rep = 0; // Number of computers repaired char success; // computer operation cin> n> d; for (int I = 1; I <= n; I ++) scanf ("% d", & coo [I]. x, & coo [I]. y); for (int I = 1; I <= n; I ++) father [I] = I; // initialize the parent to getchar (); while (scanf ("% c", & seconds )! = EOF) {if (then = 'O') {int numb; scanf ("% d", & numb); rep [num_rep]. x = coo [numb]. x; rep [num_rep]. y = coo [numb]. y; rep [num_rep]. numb = numb; for (int I = 0; I <num_rep; I ++) {if (num_rep = 0) break; int a, B; a = rep [I]. x-rep [num_rep]. x; B = rep [I]. y-rep [num_rep]. y; if (a * a + B * B <= d * d) Union (rep [I]. numb, rep [num_rep]. numb);} num_rep ++;} else {int num1, num2; scanf ("% d", & num1, & num2); if (Find_fath (num1) = Find_fath (num2) printf ("SUCCESS \ n"); else printf ("FAIL \ n") ;}getchar ();} return 0 ;}
I hope you can make some corrections.