Wireless Network
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 35815 | Accepted: 14853 |
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.
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:
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.
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.
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
并查集水题。
题意:有一个网络中心,但由于事故里面的电脑都出了故障。并正在进行紧急抢修。已知一台电脑能和它距离小于等于距离d的电脑通讯,且此通讯具有传递性,及A和B能连接,B和C能连接,则A和C能连接。
现在有一系列的查询query,O表示有一台电脑能修复,S则要求你判断两台电脑是否联通。
读入数据并建立并查集。如果是O则对所有电脑进行遍历,如果距离小于d并且该电脑被修复则进行union(一开始没注意此wa)
然后就没有了。cin要3000+ms,scanf可以优化到1000ms以内
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#define vi vector<int>
#define P pair<int,int>
using namespace std;
const int MAX_N = 4e3 + 10;
int par[MAX_N];
int Rank[MAX_N];
bool repair[MAX_N];
P p[MAX_N];
int n,d;
void init()
{
memset(Rank, 0,sizeof(Rank));
for(int i = 1; i <= n; i++)
par[i] = i;
}
int Find(int i)
{
if(par[i] == i)
return i;
return par[i] = Find(par[i]);
}
void unite(int x, int y)
{
x = Find(x);
y = Find(y);
if(Rank[x] < Rank[y])
par[x] = y;
else
{
par[y] = x;
if(Rank[x] == Rank[y])
Rank[x]++;
}
}
bool same(int x, int y)
{
return Find(x) == Find(y);
}
double dis(int x, int y)
{
double dist = (p[x].first - p[y].first) * (p[x].first - p[y].first);
dist += (p[x].second - p[y].second) * (p[x].second - p[y].second);
return sqrt(dist);
}
int main()
{
// freopen("out.txt","w",stdout);
// ios::sync_with_stdio(false);
//cin.tie(0);
scanf("%d%d", &n, &d);
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &p[i].first, &p[i].second);
}
char query[5];
init();
while(~scanf("%s",query))
{
int x, y;
if(query[0] == 'O')
{
scanf("%d", &x);
repair[x] = 1;
for(int i = 1; i <= n; i++)
{
if(i != x)
if(repair[i] == 1 && dis(i, x) <= (double)d)
unite(i,x);
}
}
else
{
scanf("%d%d", &x, &y);
if(same(x, y))
puts("SUCCESS");
else puts("FAIL");
}
}
return 0;
}