Wireless Network
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 38223 | Accepted: 15828 |
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:
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
题意:给你n台坏掉的电脑,往后是两种操作 :O, p 是修复编号为p的电脑;S,p,q是问编号为p和q的电脑能否连在一起;
转化成并查集,是否修复可以加一个vis做标记
#include <iostream>
using namespace std;
struct computer
{
int x, y;
bool vis;
int parent;
}com[1100];
int Find ( int X )
{
int i = 0;
while( i != X )
i++;
while( com[i].parent >= 0 )
i = com[i].parent ;
return i;
}
void Union ( int X1, int X2 )
{
int Root1, Root2;
Root1 = Find ( X1 );
Root2 = Find ( X2 );
if( Root1 != Root2 )
com[Root2].parent = Root1;
}
int main()
{
int n, d;
cin >> n >> d;
for( int i=1; i<=n; i++ )
{
cin >> com[i].x >> com[i].y;
com[i].vis = false;
com[i].parent = -1;
}
char order;
int p, q;
while( cin >> order )
{
if( order == 'O' )
{
cin >> p;
com[p].vis = true;
for( int i=1; i<=n; i++ )
{
if( com[i].vis == true )
{
int ax = max( com[i].x, com[p].x ) - min( com[i].x, com[p].x );
int ay = max( com[i].y, com[p].y ) - min( com[i].y, com[p].y );
if( ax*ax + ay*ay <= d*d )
Union( i, p );
}
}
}
if( order == 'S' )
{
cin >> p >> q;
if( Find( p ) == Find( q ) )
cout << "SUCCESS" << endl;
else
cout << "FAIL" << endl;
}
}
return 0;
}