挺接近模板的一题 接受O操作的时候扫一遍 符合条件的merge进去 done
#include <cstdio> #include <cstring> #include <cmath> struct Point{float x,y;}; int father[1010]; float distance(Point a, Point b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int find(int x) { while(father[x] != x) x = father[x]; return x; } void merge(int x, int y) { int tx = find(x); int ty = find(y); if(tx != ty) father[tx] = ty; } int main() { Point p[1010]; int n, d; bool open[1010]; //initiate memset(open, false, sizeof open); for(int i = 1; i < 1010; i++) father[i] = i; //save cordinates scanf("%d%d", &n, &d); for(int i = 1; i <= n; i++) scanf("%f%f", &p[i].x, &p[i].y); //accept operations char tmp[2]; while(scanf("%s",tmp)!=EOF) { if(tmp[0] == 'O'){ int node; scanf("%d", &node); open[node] = true; for(int i = 1; i <= n; i++) if(open[i] && i != node) if(distance(p[i], p[node]) <= (float)d){ //printf("distance(a,b) = %f ", distance(p[i], p[node])); //printf("a = %d b = %d ", node, i); merge(node, i); } } else if (tmp[0] == 'S'){ int a, b; scanf("%d%d",&a,&b); if(find(a) == find(b)) puts("SUCCESS"); else puts("FAIL"); } } return 0; }