In the process of repairing the network, workers can take two kinds of operations at every moment, repairing a computer, or testing if two computers can communicate. Your job is to answer all the testing operations.
1. "O p" (1 <= p <= N), which means repairing computer p.
2. "S p q" (1 <= p, q <= N), which means testing whether computer p and q can communicate.
The input will not exceed 300000 lines.
4 1 0 1 0 2 0 3 0 4 O 1 O 2 O 4 S 1 4 O 3 S 1 4Sample Output
FAIL SUCCESS
题目大意:给出各个点的坐标,以及能够取得联系的距离,判断某两个点之间能否联系
很明显的一个并查集问题,只是输入让我愣了一会,重点认为是输入吧,跟模板相比其实只是多了一个能否联系的判断
代码:
#include<stdio.h>
#include<string.h>
using namespace std;
int f[1005],book[1005];
int n,d;
struct node
{
int x;
int y;
}zuo[1005];
int longth(int a,int b)
{
if((zuo[a].x-zuo[b].x)*(zuo[a].x-zuo[b].x)+(zuo[a].y-zuo[b].y)*(zuo[a].y-zuo[b].y)<=d*d)
return 1;
else
return 0;
}
void intt()
{
for(int i=1;i<=n;i++)
f[i]=i;
}
int getf(int v)
{
if(f[v]==v)
return v;
else
{
f[v]=getf(f[v]);
return f[v];
}
}
void marge(int a,int b)
{
int t1,t2;
t1=getf(a);
t2=getf(b);
if(longth(a,b))
{
if(t1!=t2)
f[t1]=t2;
}
return ;
}
int main()
{
int i,j,k,t,t1,t2;
char a;
while(scanf("%d %d",&n,&d)!=EOF)
{
memset(book,0,sizeof(book));
intt();
for(i=1;i<=n;i++)
scanf("%d %d",&zuo[i].x,&zuo[i].y);
while(scanf("%c",&a)!=EOF)
{
if(a=='O')
{
scanf("%d",&t);
book[t]=1;
for(i=1;i<=n;i++)
{
if(book[i]==1)//由于此题不是一次性的把所有的点都用上,因此加了个记录数组
marge(t,i);//将给出的点直接联合(函数中多了一个判断)
}
}
if(a=='S')
{
scanf("%d %d",&t1,&t2);
if(getf(t1)==getf(t2))
printf("SUCCESS\n");
else
printf("FAIL\n");
}
}
}
return 0;
}
本文介绍了一种使用并查集算法解决计算机网络修复问题的方法。在地震导致网络中断后,通过逐步修复计算机节点,并检查节点间是否能建立通信。文章详细阐述了如何利用并查集进行节点连接状态的维护及查询。
554

被折叠的 条评论
为什么被折叠?



