模板
#include <bits/stdc++.h>
using namespace std;
const int max_n = 1e5 + 10;
int pre[max_n];
int findf(int root)
{
if(root == pre[root])
return root;
else{
root = findf(pre[root]);
return root;
} // 查找+压缩路径
}
void mer(int root1, int root2) //比对
{
int t1 = findf(root1);
int t2 = findf(root2);
if(t1 != t2)
{
pre[t1] = t2;
}
}
int main()
{
int n;
cin>>n;
for(int i = 1; i <= n ;i++)
pre[i] = i;
}
https://cn.vjudge.net/problem/POJ-2236
#include <iostream>
#include <memory.h>
#include <cstdio>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
int pre[1005];
struct node
{
double x, y;
}s[1005];
bool vis[1005];
int findf(int root)
{
if(pre[root] == -1)
return -1;
else if(root == pre[root])
return root;
else{
root = findf(pre[root]);
return root;
}
}
void mer(int root1, int root2)
{
int t1 = findf(root1);
int t2 = findf(root2);
if(t1 != t2)
{
pre[t1] = t2;
}
}
int main()
{
int n, d;
scanf("%d%d", &n, &d);
mem(vis, false);
mem(pre, -1);
for(int i = 1; i <= n; i++)
{
scanf("%lf%lf", &s[i].x, &s[i].y);
}
getchar();
char jud;
while(~scanf("%c", &jud))
{
if(jud == 'S'){
int data1, data2;
scanf("%d%d", &data1, &data2);
getchar();
int t1 = findf(data1);
int t2 = findf(data2);
if(t1 == t2)
printf("SUCCESS\n");
else
printf("FAIL\n");
}
else{
int data;
scanf("%d", &data);
getchar();
vis[data] = true;
pre[data] = data;
for(int i = 1; i <= n; i++)
{
if(vis[i]&&i!=data){
double x = s[i].x-s[data].x;
double y = s[i].y-s[data].y;
if(x*x+y*y<=d*d)
mer(i, data);
}
}
}
}
return 0;
}