zoukankan      html  css  js  c++  java
  • poj2236 (并查集)

    Wireless Network
    Time Limit: 10000MS   Memory Limit: 65536K
    Total Submissions: 38915   Accepted: 16135

    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
    
    分析:并查集。
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int x[1010],y[1010];
    int dis[1004][1004];
    int pre[1010],vis[1010];
    int N,D;
    void init()
    {
        for(int i=0;i<=N;i++)
            pre[i]=i;
    }
    
    int find(int x)//查找点x所在树的根 
    {
        int root=x;
        while(pre[root]!=root)
            root=pre[root];
        int i=x,k;
        while(k!=root)//路径压缩,让x到根经过的所有节点直接指向root
        {
            k=pre[i];
            pre[i]=root;
            i=k;
        }
        return root;
    }
    
    void unit(int x,int y)
    {
        int rootx=find(x),rooty=find(y);
        if(rootx!=rooty)
        {
            //if(rootx<rooty)
        //        pre[rooty]=pre[rootx];
            //else 
                pre[rootx]=pre[rooty];
        }
    }
    
    void Opre(int p)//修复电脑p
    {
        for(int i=1;i<=N;i++)
        if(vis[i]&&dis[p][i]<=D&&i!=p)//找孤立的点,root也算 
        {
            unit(i,p);
        }
    }
    
    int main()
    {
        scanf("%d%d",&N,&D);
        init();
        for(int i=1;i<=N;i++)
        {
            scanf("%d%d",&x[i],&y[i]);    
        }
        D=D*D;
        for(int i=1;i<N;i++)
        for(int j=i+1;j<=N;j++)//两点间距离 
            dis[j][i]=dis[i][j]=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
        char s[10]; 
        while(scanf("%s",s)!=EOF)
        {
            if(s[0]=='O')
            {
                int p;
                scanf("%d",&p);//修复电脑p
                vis[p]=1;
                Opre(p);
                //for(int i=1;i<=N;i++)
                //printf("pre[%d]=%d
    ",i,pre[i]);
            }
            else
            {
                int p,q;
                scanf("%d%d",&p,&q);//尝试联系电脑p和q 
                if(find(p)==find(q))
                    printf("SUCCESS
    ");
                else printf("FAIL
    ");
            }
            
        }
        
        return 0;
    }
    View Code



  • 相关阅读:
    QTP 处理webtable中的数据
    Error 1406.Setup cannot write the value Microsoft
    QTP打开WinTree中的指定节点
    Excel数据操作
    VBS中实现函数多返回值
    QTP的回放模式
    VBS中运行应用程序的两种方式及WshShell对像浅析
    程序员奇葩面试的奇葩问题
    Android如何实现毛玻璃效果之Android高级模糊技术
    Android通过用代码画虚线椭圆边框背景来学习一下shape的用法
  • 原文地址:https://www.cnblogs.com/ACRykl/p/9693484.html
Copyright © 2011-2022 走看看