来源poj2236
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
额,一开始题目没看清,其实就是简单的并查集,用一个vector存一下哪些编号被修好了,然后查一下哪些是可以和这个计算机相连的,然后连起来就好了
代码因为一开始没搞清 写的不好
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<stack>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+100;
const double eps=1e-8;
using namespace std;
const double pi=acos(-1.0);
const int inf=0xfffffff;
const int N=10005;
struct PC
{
int x,y,f,temp;
}pc[N];
vector<int>v;
int l;
int find(int x)
{
if(pc[x].f==x)
return x;
return pc[x].f=find(pc[x].f);
}
double dist(int a,int b)
{
return sqrt(1.0*(pc[a].x-pc[b].x)*(pc[a].x-pc[b].x)+(pc[a].y-pc[b].y)*(pc[a].y-pc[b].y));
}
void Union(int x)
{
pc[x].temp=1;
rep(i,0,v.size())
{
if(v[i]!=x)
if(dist(v[i],x)<=l+eps)
{
pc[find(v[i])].f=x;
}
}
v.push_back(x);
}
bool judge(int a,int b)
{
if(pc[a].temp==0||pc[b].temp==0) return false;
if(dist(a,b)<=l+eps) return true;
int fa=find(a),fb=find(b);
if(fa==fb) return true;
return false;
}
int main()
{
int n,x,y;
char c;
v.clear();
cin>>n>>l;
rep(i,1,n+1)
{
sf("%d%d",&pc[i].x,&pc[i].y);
pc[i].f=i;pc[i].temp=0;
}
getchar();
while(sf("%c",&c)!=EOF)
{
if(c=='O')
{
sf("%d",&x);
Union(x);
}else
{
sf("%d%d",&x,&y);
if(judge(x,y)) pf("SUCCESS
");
else pf("FAIL
");
}
getchar();
}
return 0;
}