A - Wireless Network
POJ - 2236Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 50348 | Accepted: 20619 |
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
[Submit] [Go Back] [Status] [Discuss]
题意
有n个坏掉的卫星,每次操作‘O’可以修好一个,如果卫星之间都是‘修好的’状态且间距小于‘d',就可以通讯。
思路
并查集的时候判断一下两卫星之间距离即可。
CODE
1 #include2 #include 3 #include 4 #include 5 #include 6 7 #define dbg(x) cout << #x << "=" << x << endl 8 9 using namespace std; 10 const int maxn = 1007; 11 12 int fa[maxn], dis[maxn]; 13 int n,m,ans,cnt; 14 int d; 15 int canuse[maxn]; 16 17 struct node { 18 int x, y; 19 }a[maxn]; 20 21 void init() 22 { 23 for(int i = 1; i <= n; i++) { 24 fa[i] = i; 25 } 26 } 27 28 double cal(node a, node b) { 29 return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y) * (a.y - b.y)); 30 } 31 32 int fid(int x) 33 { 34 int r = x; 35 while(fa[r] != r) { 36 //if(cal(a[fa[r]],a[r]) <= d) 37 r = fa[r]; 38 } 39 int i,j;///路径压缩 40 i = x; 41 while(fa[i] != r) { 42 //if(cal(a[fa[i]],a[r]) <= d) { 43 j = fa[i]; 44 fa[i] = r; 45 i = j; 46 47 } 48 return r; 49 } 50 51 void join(int r1, int r2)///合并 52 { 53 int fidroot1 = fid(r1), fidroot2 = fid(r2); 54 if(fidroot1 != fidroot2) { 55 fa[fidroot2] = fidroot1; 56 } 57 } 58 59 int main() 60 { 61 scanf("%d %d",&n, &d); 62 init(); 63 for(int i = 1; i <= n; ++i) { 64 scanf("%d %d",&a[i].x, &a[i].y); 65 } 66 getchar(); 67 char ch[2]; 68 int p,q; 69 int cnt = 1; 70 while(scanf("%s", ch) != EOF) { 71 //dbg(ch[0]); 72 if(ch[0] == 'O') { 73 scanf("%d",&p); 74 //getchar(); 75 canuse[cnt++] = p; 76 for(int i = 1; i < cnt-1; ++i) { 77 if(cal(a[canuse[i]],a[p]) <= double(d)) { 78 join(canuse[i],p); 79 } 80 } 81 } 82 if(ch[0] == 'S') { 83 scanf("%d %d",&p,&q); 84 //getchar(); 85 86 //join(p,q); 87 if(fid(p) == fid(q)) { 88 puts("SUCCESS"); 89 } 90 else { 91 puts("FAIL"); 92 } 93 } 94 } 95 return 0; 96 }