题意:给你n个点,有若干次询问,O代表修补好的电脑,S代表询问两点之间是否可以communicate。
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN=100005;
int bin[MAXN];
struct node{
int u, v, w;
}edge[MAXN];
int num[MAXN];
int n;
void init()
{
for(int i=1;i<=n;++i)
bin[i]=i;
}
int findx(int x)
{
if(x==bin[x])
return bin[x];
return bin[x]=findx(bin[x]);
}
bool cmp(node a, node b)
{
return a.w>b.w;
}
int unionset(int s1, int s2)
{
if(s1==s2)return -1;
if(num[s1]>num[s2])
bin[s2]=s1,num[s2]=0;
else
bin[s1]=s2,num[s1]=0;
return 0;
}
int main()
{
int m;
scanf("%d %d", &n, &m);
init();
for(int i=0;i<m;++i)
scanf("%d %d %d", &edge[i].u, &edge[i].v, &edge[i].w);
sort(edge, edge+m, cmp);
//int cost=0;
for(int i=1;i<=n;++i)
num[i]=1;
int cnt=0;
int ans=0;
for(int i=0;i<m;++i)
{
int pu, pv;
pu=findx(edge[i].u);
pv=findx(edge[i].v);
if(pu!=pv)
{
bin[pu]=pv;
num[cnt++]=edge[i].w;
}
}
ans=unique(num, num+cnt)-num;
printf("%d\n", ans);
return 0;
}
*/
#include<cstring>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int MAXN = 1005;
struct NODE
{
int x, y, pre;
};
int bin[MAXN];
bool vis[MAXN];
NODE p[MAXN];
int d;
int findx(int x)
{
if(p[x].pre==x)
return p[x].pre;
return p[x].pre=findx(p[x].pre);
//return p[x].pre==x?x:findx(p[x].pre);
}
void join(NODE p1, NODE p2)
{
int root1, root2;
root1 = findx(p1.pre);
root2 = findx(p2.pre);
if(root1!=root2)
if((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)<=d*d)
p[root1].pre=root2;
}
int main()
{
int n , i, ok, from, to;
char ch;
scanf("%d %d", &n, &d);
for(i=1;i<=n;i++)
p[i].pre = i;
for(i=1; i<=n; i++)
scanf("%d %d", &p[i].x, &p[i].y);
memset(vis, false ,sizeof(vis));
while(~scanf(" %c", &ch))
{
if(ch=='O')
{
scanf("%d", &ok);
vis[ok]=true;
for(i=1;i<=n;i++)
{
if(vis[i]&&i!=ok)
join(p[i], p[ok]);//将已知修好的电脑合并在一个集合内
}
}
else
{
scanf("%d%d", &from, &to);
if(findx(from)==findx(to))//判断是否属于同一个集合
printf("SUCCESS\n");
else
printf("FAIL\n");
}
}
return 0;
}