POJ2236 解题报告

来源:互联网 发布:巴洛克的艺术风格 知乎 编辑:程序博客网 时间:2024/06/06 00:55
Wireless Network
Time Limit: 10000MSMemory Limit: 65536KTotal Submissions: 28851Accepted: 11950

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 10 10 20 30 4O 1O 2O 4S 1 4O 3S 1 4

Sample Output

FAILSUCCESS

Source

POJ Monthly,HQM

这题比较简单,不过第一次提交的时候把 FAIL 写成了 FALL ,结果就wa了


做法:并查集

//Asia Cooperated Medical team//POJ 2236#include<iostream>#include<algorithm>using namespace std;struct POINT        //表示一个点,记录该点的坐标,是否被修复{    int x;    int y;    bool repaired;};const int maxn=1001+100;const int maxd=20000+200;int par[maxn];    //父节点坐标int rnk[maxn];    //树的高度POINT point[maxn];int N,D;bool can_communicate(int a,int b); //判断是否可以联通void net_working(int p);    //将p点连入它能够连入的网络void init();                //初始化父节点、高度、将每个点的修复状态标记为falsevoid unit(int a,int b);     //将两个点连到一个网络中int fin(int a);             //寻找a点的父节点int main(){    cin>>N>>D;    init();    for(int i=1; i<=N; i++)        cin>>point[i].x>>point[i].y;    char order;    while(cin>>order)    {        if(order=='O')  //修复命令        {            int p;            cin>>p;            point[p].repaired=true;  //标记p点被修复            net_working(p);          //连入网络        }        else  //查询命令        {            int p1,p2;            cin>>p1>>p2;            if(can_communicate(p1,p2)) cout<<"SUCCESS\n";            else cout<<"FAIL\n";        }    }    return 0;}void net_working(int p){    for(int i=1; i<=N; i++)    {        if(i==p) continue;        int xx=(point[p].x-point[i].x)*(point[p].x-point[i].x);        int yy=(point[p].y-point[i].y)*(point[p].y-point[i].y);        if(xx+yy<=D*D&&point[i].repaired) unit(p,i);    //如果在最短范围内有已经被修复的点,那么就和这个点连接    }}void init(){    for(int i=1; i<=N; i++)    {        par[i]=i;        rnk[i]=0;        point[i].repaired=false;    }}void unit(int a,int b){    a=fin(a);    b=fin(b);    if(a==b) return ;    if(rnk[a]<rnk[b]) par[a]=par[b];    else    {        par[b]=par[a];        if(rnk[a]==rnk[b])            rnk[a]++;    }}int fin(int a){    if(par[a]==a) return a;    else return par[a]=fin(par[a]);}bool can_communicate(int a,int b){    return fin(a)==fin(b);}