poj 2236 Wireless Network(并查集)(有点暴力)

来源:互联网 发布:软件项目管理发展前景 编辑:程序博客网 时间:2024/06/11 10:25

Wireless Network

Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 30730 Accepted: 12787
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


给出计算机的坐标,和两台计算机直接通信的最大距离,O 后接的是修复好的计算机,S询问两个计算机之间能否通信。


看到数据那么大,当时就觉得暴力会超时,就去找两点之间的距离简化,但是没有看到,直接for循环找公共根节点又怕时间用太多了。。。
还是用了三秒多过了


#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>using namespace std;#define N 1111//double _sqrt;int n, m, a, b, cnt;int p[N], way[N];struct node{    int xx, yy;}nod[N];int fnd( int x ) { return p[x] = p[x] == x ? x : fnd( p[x] ); }bool same( int x, int y ) { return fnd(x) == fnd(y); }bool check( node x, node y ) { return ( x.xx-y.xx )*( x.xx-y.xx ) + ( x.yy-y.yy )*( x.yy-y.yy ) <= m*m; }///void unit( int x, int y ){    int u = fnd( x );    int v = fnd( y );    if( u != v ){        p[u] = y;    }}int main(){    //freopen( "in.txt", "r", stdin );    char ch;    while( scanf( "%d%d", &n, &m ) != EOF ){        memset( way, 0, sizeof( way ));        //_sqrt = (double)sqrt( m*1.0*m/2 );        for( int i = 0 ; i <= n ; i ++ ) p[i] = i;        for( int i = 1 ; i <= n ; i ++ ){            scanf( "%d%d", &nod[i].xx, &nod[i].yy );        }        cnt = 0;        while( scanf( "%c", &ch ) != EOF ){            if( ch == 'O' ){                scanf( "%d", &a );                way[cnt++] = a;///修复好的计算机                for( int i = 0 ; i < cnt-1 ; i ++ ){                    if( check( nod[a], nod[way[i]] ) ){                        unit( a, way[i] );                    }                }            }            else if( ch == 'S' ){                scanf( "%d%d", &a, &b );                if( same( a, b ) )                    printf( "SUCCESS\n" );                else                    printf( "FAIL\n" );            }        }    }    return 0;}
原创粉丝点击