Wireless Network
Description
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
Source
POJ Monthly,HQM
[Submit]
[Go Back]
[Status]
[Discuss]
题意
有N个点,如果有两个点之间的距离小于等于D,那么这两个点就可以连起来,接下来N行,是从1到N点的坐标,接下来两种输入方式:
- O p,代表p点已被修复
- S p q,查询p点和q点能否通讯
思路
将能进行通讯的相连,查询两点的父亲节点是否相同即可判断两点是否连接。
PS
这道题有些看脸,注意一下。交了相同的代码,一次TLE,一次AC。
#include<iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <algorithm>
#define ll long long
#define PI acos(-1)
#define mes(x,y) memset(x,y,sizeof(x))
#define FOR(i,a,b) for(long long i = a;i <= b;++i)
#define ROF(i,a,b) for(long long i = a;i >= b;--i)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const ll inf = 70000;
const ll mod = 1e9;
ll n, m, i, j, k = 0, t, w, flag;
double x, y, z;
string s1, s2, s;
struct node {
double x, y;//坐标
ll fa, iswork;
//父亲节点,是否可以通讯
}father[1010];
ll find(ll x) {
return x == father[x].fa ? x : father[x].fa = find(father[x].fa);
}//查询此点的父亲是谁
void connect(ll y) {
double len = sqrt((father[t].x - father[y].x) * (father[t].x - father[y].x) + (father[t].y - father[y].y) * (father[t].y - father[y].y));
if (father[y].iswork == 0 || len > m)return;
ll xx = find(t), yy = find(y);
if (xx != yy)father[yy].fa = xx;
}
int main() {
while (cin >> n >> m) {
for (i = 1; i <= n; i++) {
cin >> x >> y;
father[i].x = x;
father[i].y = y;
father[i].fa = i;
father[i].iswork = 0;
}
while (cin >> s) {
if (s == "O") {
cin >> t;
father[t].iswork = 1;
for (i = 1; i <= n; i++) connect(i);
}
else {
cin >> t >> w;
cout << (find(t) == find(w) ? "SUCCESS\n" : "FAIL\n");
}
}
}
return 0;
}