Test instructions: There are n computers broken, numbered from 1 to N, now to repair the computer and to enable communication between the computer, if the two repaired computer between the direct distance of less than equal to D can communicate, if the distance between computer A and computer C is greater than D but a and C can communicate with Computer B, then A and C can communicate. O A, fix computer A. S a B asks if A and B can communicate.
The puzzle: Every time a computer is repaired, it will go through all the computers and put the repaired and able-to-communicate computer into a set, asking if you can determine whether it is within the set.
#include <stdio.h>#include <math.h>Const intN =1005;structPintx, y;} P[n];intN, Pa[n], vis[n], D;Charstr[5];intCount (p A, p b) {return(a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y);}intGet_parent (intx) {returnx = = Pa[x]? X:PA[X] = get_parent (Pa[x]);}intMain () {scanf("%d%d", &n, &d); for(inti =1; I <= N; i++) {scanf("%d%d", &p[i].x, &P[I].Y); Pa[i] = i; } while(scanf('%s ', str)! = EOF) {if(str[0] ==' O ') {intAscanf("%d", &a); Vis[a] =1; for(inti =1; I <= N; i++)if(I! = a && vis[i] && Count (P[a], p[i]) <= d * d) {intpx = Get_parent (a);intPY = get_parent (i);if(px! = py) {PA[PX] = py; } } }Else{intA, B;scanf("%d%d", &a, &b);if(Get_parent (a) = = Get_parent (b))printf("success\n");Else printf("Fail\n"); } }return 0; }
POJ 2236 (and check set)