poj2236 (Wireless Network) and check set

Source: Internet
Author: User

Description

An earthquake takes place in southeast Asia. The ACM (Asia cooperated Medical team) has set up a wireless network with the lap computers, but an unexpected aftershock Attacked, all computers on the network were all broken. The computers is 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 is not farther tha n d meters from it. But every computer can be regarded as the intermediary of the communication between and the other computers, that's to say Co Mputer A and Computer B can communicate if computer A and computer B can communicate directly or there is A computer C tha T can communicate with both A and B.

In the process of repairing the network, workers can take the other kinds of operations at every moment, repairing a computer, O R Testing if computers can communicate. Your job is to answer all the testing operations.

Input

The first line contains the integers n and d (1 <= n <= 1001, 0 <= D <= 20000). Here N is the number of computers, which was numbered from 1 to N, and D is the maximum distance the computers can Communi Cate directly. In the next N lines, each contains. Integers xi, yi (0 <= xi, Yi <= 10000), which is the coordinate of N computer S. from the (n+1)-th line to the end of input, there is operations, which is carried out one by one. Each line contains a operation in one of following and 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 would not be exceed 300000 lines.

Output

For each testing operation, print "SUCCESS" if the both computers can communicate, or "FAIL" if not.

Sample Input

4 4O 1O 2O 4S 1 4O 3S 1 4

Test instructions: give you the coordinates of n bad computer, if two repaired computer distance is not greater than D, they connect. Two computers can be connected indirectly via a third computer. Give a number of queries, one is to repair which computer, one is to ask whether two computers connected.


Puzzle: Typical and check set, I did not add rank array optimization When merging, you can add it yourself, but 10s time more than enough.


#include <iostream> #include <sstream> #include <fstream> #include <string> #include <map > #include <vector> #include <list> #include <set> #include <stack> #include <queue># Include <deque> #include <algorithm> #include <functional> #include <iomanip> #include < limits> #include <new> #include <utility> #include <iterator> #include <cstdio> #include < cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <ctime>using  namespace Std;const int maxn = 1010;int f[maxn];int px[maxn], Py[maxn];int vis[maxn];int N, d;int Find (int x) {return x = = F[x]? x: (F[x] = Find (f[x));}    void join (int x, int y) {int fx = find (x), FY = find (y); F[FX] = fy;} int dis (int x, int y) {return (Px[x]-px[y]) * (Px[x]-px[y]) + (Py[x]-py[y]) * (Py[x]-py[y]);}    int main () {CIN >> n >> D; for (int i = 1; I <= n; ++i) {scanf ("%d%d", &px[i], &amP;py[i]);    F[i] = i;    } memset (Vis, 0, sizeof (VIS));    Char op[5];            while (~SCANF ("%s", op)) if (op[0] = = ' O ') {int p;            scanf ("%d", &p);            VIS[P] = 1;                    for (int i = 1; I <= n; ++i) if (i! = P && vis[i] && dis (i, p) <= d*d)        Join (I, p);            } else {int x, y;            scanf ("%d%d", &x, &y);            int FX = FIND (x), FY = find (y);            if (FX = = FY) printf ("success\n");        else printf ("fail\n"); } return 0;}


Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.

poj2236 (Wireless Network) and check set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.