Problem Links:
poj2236,
Problem:
Wireless Network
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 9714 | Accepted: 4110 |
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
Solution:
Disjoint set structure, using find-union algorithms.
PS:
1st, take care of the union function, since the parent can be changed any time.
2nd, use the original index rather than using the root index. It won't make you efficient, it just make your code from AC to WA.
Source Code:
//Sat Jun 5 11:26:21 CDT 2010#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define MMAX 1002
using namespace std;
class Networks
{
public:
int x;
int y;
int parent;
int rank;
bool visited;
};
vector<Networks> v(MMAX);
void Makeset(int N)
{
for (int i = 1; i <= N; i++)
{
int x, y;
cin >> x >> y;
v[i].x = x;
v[i].y = y;
v[i].parent = i;
v[i].rank = 1;
v[i].visited = false;
}
}
int Find(int x)
{
if (x != v[x].parent)
v[x].parent = Find(v[x].parent);
return v[x].parent;
}
void Union(int x, int y)
{
int t1 = Find(x);
int t2 = Find(y);
if (t1 == t2)
return;
if (v[t1].rank > v[t2].rank)
v[t2].parent = t1;
else
{
v[t1].parent = t2;
if (v[t1].rank == v[t2].rank)
v[t2].rank++;
}
}
bool check(int a, int b, long dist)
{
double d = (v[a].x - v[b].x) * (v[a].x - v[b].x) + (v[a].y - v[b].y)
* (v[a].y - v[b].y);
d = sqrt(d);
return d <= dist ? true : false;
}
int main(int argc, const char* argv[])
{
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
int N;
long D;
cin >> N >> D;
Makeset(N);
string c;
int a, b;
while (cin >> c >> a)
{
if (c[0] == 'O')
{
for (int i = 1; i <= N; i++)
{
if (v[i].visited && i != a && check(i, a, D))
{
Union(i, a);
}
}
v[a].visited = true;
}
else
{
cin >> b;
int t1 = Find(a);
int t2 = Find(b);
if (check(a, b, D) || t1 == t2)
{
cout << "SUCCESS" << endl;
// Union(a, b);
}
else
cout << "FAIL" << endl;
}
}
fclose(stdin);
fclose(stdout);
return 0;
}
No comments :
Post a Comment