poj 2236

#include<stdio.h>
#include<math.h>
#include<string.h>
#define N 1100
struct node {
	int x,y;
}map[N];
int s[N];
int pre[N];
int find(int n) {
	if(n!=pre[n])
		pre[n]=find(pre[n]);
	return pre[n];
}
int main() {
      int n,m,i,a,b,f1,f2;
	  char  ch[30];
	  int e;
	  while(scanf("%d%d",&n,&m)!=EOF) {
		  for(i=1;i<=n;i++)
			  pre[i]=i;
		  for(i=1;i<=n;i++)
          scanf("%d%d",&map[i].x,&map[i].y);
		  memset(s,0,sizeof(s));
		  while(scanf("%s",ch)!=EOF) {
			  if(ch[0]=='O') {
				  scanf("%d",&a);
				  for(i=1;i<=n;i++) {
					  if(s[i]==0)
						  continue;
					  if(i==a)
						  continue;
					 e=(map[a].x-map[i].x)*(map[a].x-map[i].x)+(map[a].y-map[i].y)*(map[a].y-map[i].y);
					 if(e<=m*m) {
						 f1=find(i);
						 f2=find(a);
						 pre[f1]=f2;
				  }
			
			  }	 
				  s[a]=1;
			  }
			 else {
				   scanf("%d%d",&a,&b);
				   if(find(a)==find(b))
					   printf("SUCCESS
");
				   else
					   printf("FAIL
");
			  }
		  }
		  printf("
");
	  }
	  return 0;
}
原文地址:https://www.cnblogs.com/thefirstfeeling/p/4410916.html