题意描述:
电脑是否能相互连接问题,根据当前修整情况问两台电脑是否可以连接(距离不超过d可以是间接连接)
解题思路:
这个题需要用先将各节点距离信息存入一个二维数组,然后要是遇到对电脑的修整就开始建树,(中间需要加上对于条件的判断具体的看代码中的描述),遇到字符串首字符为S就判断这两个节点是否在是同一个根节点(可以直接或间接连接起来),可以的话输出SUCCESS不行的话输出FAIL
注意:
Sqrt在这个oj上只能对于double类型的进行运算,对于每组数据标记的时候都要交进行归0处理,还有就是在进行建树的时候需要有多重的判断,要判断当前节点i是否在树上并且i与a不能相等并且e[i][a]<d,其他的套用模板就行
题目:
An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers are repaired one by one, and the network gradually began to work again. Because of the hardware restricts, each computer can only directly communicate with the computers that are not farther than d meters from it. But every computer can be regarded as the intermediary of the communication between two other computers, that is to say computer A and computer B can communicate if computer A and computer B can communicate directly or there is a computer C that can communicate with both A and B.
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.
Input
The first line contains two integers N and d (1 <= N <= 1001, 0 <= d <= 20000). Here N is the number of computers, which are numbered from 1 to N, and D is the maximum distance two computers can communicate directly. In the next N lines, each contains two integers xi, yi (0 <= xi, yi <= 10000), which is the coordinate of N computers. From the (N+1)-th line to the end of input, there are operations, which are carried out one by one. Each line contains an operation in one of following two formats:
- “O p” (1 <= p <= N), which means repairing computer p.
- “S p q” (1 <= p, q <= N), which means testing whether computer p and q can communicate.
The input will not exceed 300000 lines.
Output
For each Testing operation, print “SUCCESS” if the two computers can communicate, or “FAIL” if not.
Sample Input
4 1
0 1
0 2
0 3
0 4
O 1
O 2
O 4
S 1 4
O 3
S 1 4
Sample Output
FAIL
SUCCESS
AC代码;
#include<stdio.h>
#include<string.h>
#include<math.h>
int n,d;
int f[2000];
double e[2000][2000],vis[2000];
struct node{
double x;
double y;
};
void init()
{
int i;
for(i=1; i<=n; i++)
f[i]=i;
return ;
}
int getf(int v)
{
if(f[v]==v)
return v;
else
{
f[v]=getf(f[v]);
return f[v];
}
}
void merge(int v,int u)
{
int t1,t2;
t1=getf(v);
t2=getf(u);
if(t1!=t2)
{
f[t2]=t1;
}
return ;
}
int main()
{
int i,j,a,b;
char op[6];
struct node s[2000];
while(scanf("%d %d", &n,&d)!=EOF)
{
init();
for(i=1; i<=n; i++)
scanf("%lf %lf", &s[i].x ,&s[i].y);
for(i=1; i<=n; i++)//存储节点之间的距离
{
for(j=1; j<=n; j++)
{
e[i][j]=sqrt((s[i].x-s[j].x)*(s[i].x-s[j].x)
+(s[i].y-s[j].y)*(s[i].y-s[j].y));
}
}
memset(vis,0,sizeof(vis));
while(scanf("%s", op)!=EOF)
{
if(op[0]=='O')
{
scanf("%d", &a);
vis[a]=1;//表示a电脑已经修过
for(i=1; i<=n; i++)
{
if(vis[i]&&i!=a)//vis[i]已在树的结构上且两者之间距离<=d
{
if(e[i][a]<=d)
merge(a,i);
}
}
}
else
{
scanf("%d %d", &a,&b);
if(getf(a)==getf(b))
printf("SUCCESS\n");
else
printf("FAIL\n");
}
}
}
return 0;
}