Wireless Network
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 20663 | Accepted: 8689 |
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
竟然一次就过了,好激动啊!!!
题目大意:首先输入四个坐标(刚开始......一直不明白这两个数字是什么,想了一下才知道这个是坐标)。其他的应该都还好。
遇到的问题和思路:
这道题的难点就是如何创建树根,什么时候放到一棵树上去,什么时候该判断。
首先,把所有树根初始化,然后每当出现一个O,就说明已经修好了,那就先定义一个数组bool,刚开始为false,后来变成了true。用两点间距离公式,判断能否两个点变成一棵树。具体的看代码吧。
给出代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
const int MAX = 1000 + 5;
int n, d, xiu, lian1, lian2;
struct point{
int x, y;
}dian[MAX];
int par[MAX],rank[MAX];
char p;
bool us[MAX];//用来判断是否维修过
void inti(){//初始化
for(int i = 1; i <= n; i++){
par[i] = i;
rank[i] = 0;
}
}
int find(int i){//寻找树根
if(par[i] == i)return i;
else{
return par[i] = find(par[i]);
}
}
int same(int x, int y){//是同一个根,才能相互走
return find(x) == find(y);
}
void conbi(int x, int y){//连接
x = find(x);
y = find(y);
if(x == y)return;
if(rank[y] > rank[x]){
par[x] = y;
}
else {
par[y] = x;
if(rank[x] == rank[y]) rank[x]++;
}
}
void judge1(){//用来连接树根的
for(int i = 1; i <= n; i++){
if(xiu == i)continue;
if(us[i] == true){
if(same(i, xiu))continue;
else{
int g = (dian[i].x - dian[xiu].x)*(dian[i].x - dian[xiu].x) + (dian[i].y - dian[xiu].y)*(dian[i].y - dian[xiu].y);
if(g <= d*d)conbi(i, xiu);
else continue;
}
}
}
}
void judge(){//用来判断是否不是树根的情况下还能够链接
int g = (dian[lian1].x - dian[lian2].x)*(dian[lian1].x - dian[lian2].x) + (dian[lian1].y - dian[lian2].y)*(dian[lian1].y - dian[lian2].y);
if(g > d * d)printf("FAIL\n");
else printf("SUCCESS\n");
}
int main(){
while(scanf("%d%d", &n, &d)!=EOF){
getchar();
memset(dian, 0, sizeof(dian));
memset(par, 0, sizeof(par));
memset(rank, 0, sizeof(rank));
memset(us, 0, sizeof(us));
inti();
for(int i = 1; i <= n; i++){
scanf("%d%d", &dian[i].x, &dian[i].y);
getchar();
}
while(scanf("%c",&p)!=EOF){
getchar();
if(p == 'O'){
scanf("%d",&xiu);
getchar();
us[xiu] = true;
judge1();
}
else if(p == 'S'){
scanf("%d%d", &lian1, &lian2);
getchar();
if(same(lian1, lian2)){
printf("SUCCESS\n");
}
else {
judge();
}
}
}
}
return 0;
}