链接:https://vjudge.net/contest/66964#problem/A
题目
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.
输入
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.
输出
For each Testing operation, print “SUCCESS” if the two computers can communicate, or “FAIL” if not.
题意
现在有N台电脑报废了,它们分别处于(xi,yi)位置。现规定如果两台可用电脑的距离不大于d,则代表两台电脑可以联机工作。
如果A和B联机,B和C联机,则A和C也可以联机工作。
输入第一行两个数字N,d含义如上。
接下来N行每行有两个数字,代表第i台电脑的坐标。
然后会给出多组操作,每一组首先会输入一个字符。
- 如果是字母‘O’,接下来会输入一个数字,代表中央会派出人员修好这台电脑;
- 如果是字母‘S’,接下来会输入两个数字u,v,询问u,v是否可以联机工作。
思路
简单的并查集问题。
实际上我用了1个半小时,原因是在我写完之后发现不知为何,我的并查集更新出现了错误。
一个小时之后,我找到了答案:有一个地方的==
我写成了=
。
直接给代码,伊丽莎白!!!
代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
int x,y;
} a[1010];
int fa[1010];
bool vis[1010];
int findf(int x)
{
if(fa[x]==x) //就是这个地方困扰了我1个多小时,我眼瞎
{
return x;
}
else
{
return fa[x]=findf(fa[x]);
}
}
void unite(int x,int y)
{
x=findf(x);
y=findf(y);
if(x!=y)
fa[x]=y;
else
return ;
}
int main()
{
int n,m;
cin>>n>>m;
memset(vis,false,sizeof(vis));
for(int i=1; i<=n; i++)
scanf("%d%d",&a[i].x,&a[i].y);
for(int i=1; i<=n; i++)
fa[i]=i;
char s;
int pos;
while(~scanf(" %c",&s))
{
if(s=='O')
{
scanf("%d",&pos);
vis[pos]=true;
for(int i=1; i<=n; i++)
{
if(vis[i]==true&&i!=pos)
{
if((a[pos].x-a[i].x)*(a[pos].x-a[i].x)+(a[pos].y-a[i].y)*(a[pos].y-a[i].y)<=m*m) //看距离够不够
{
unite(pos,i);
}
}
}
}
if(s=='S')
{
int u,v;
scanf("%d%d",&u,&v);
if(findf(u)==findf(v))
printf("SUCCESS\n");
else
printf("FAIL\n");
}
}
}
“男人啊,只要记得1就能活下去了。”
这么看来,还要记住双等号/(ㄒoㄒ)/~~
下次再见~~