Because the number of queries may be many, interrelated computers may also be many, and the relationship between each computer only merge relationship and query, matching and check set characteristics.
And look for a more biased template point, there is no difficulty in thinking, as long as the grasp of the need to use and check the set of conditions is good.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < Queue> #include <map>using namespace Std;int n,d,vis[1111],rankk[1010],par[1020];struct Point {int x, y; Point (int x = 0,int y = 0): x (x), Y (y) {} bool operator < (const point& r) Const {return x<r.x | | x==r.x &am p;& y<r.y; }} a[1111];void init (int n) {for (int i=1;i<=n;i++) {Par[i] = i; Rankk[i] = 0; }}int findd (int x) {return par[x]==x? x:par[x] = Findd (Par[x]);} void Unite (int x,int y) {x = FINDD (x); y = findd (y); if (x==y) return; if (Rankk[x]<rankk[y]) {par[x] = y; } else {par[y] = x; if (rankk[x] = = Rankk[y]) rankk[x]++; }}bool Same (int x,int y) {return findd (x) = = Findd (y);} int main () {memset (vis,0,sizeof (VIS)); scanf ("%d%d", &n,&d); for (int i=1;i<=n;i++) {scanf ("%d%d", &a[i].x,&a[i].y); } Char s[10]; Init (n); int b,c; while (scanf ("%s", s)!=eof) {if (s[0]== ' O ') {scanf ("%d", &b); VIS[B] = 1; for (int i=1;i<=n;i++) {if (vis[i]&&i!=b) {int dd = (a[i].x-a[b].x) * (a[i].x-a [b].x] + (A[I].Y-A[B].Y) * (A[I].Y-A[B].Y); if (dd<= (d*d)) unite (B,I); }}}} else {scanf ("%d%d", &b,&c); if (Same (b,c)) printf ("success\n"); else printf ("fail\n"); }} return 0;}
Wireless Network (POJ-2236) (also set)