zoukankan      html  css  js  c++  java
  • Day5

    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

    思路:简单的并查集问题,将每一个修好的与先前修好的判断是否可以Union即可
    const int maxm = 1024;
    
    int fa[maxm], N, repair[maxm];
    double D, x[maxm], y[maxm];
    
    void init() {
        for(int i = 1; i <= N; ++i)
            fa[i] = i;
    }
    
    int Find(int x) {
        if(fa[x] == x)
            return x;
        return fa[x] = Find(fa[x]);
    }
    
    void Union(int x, int y) {
        x = Find(x);
        y = Find(y);
        if(x != y)
            fa[x] = y;
    }
    
    bool check(int u, int v) {
        double tmp = sqrt((x[u]-x[v])*(x[u]-x[v])+(y[u]-y[v])*(y[u]-y[v]));
        return tmp <= D;
    }
    
    int main() {
        scanf("%d%lf", &N, &D);
        init();
        for(int i = 1; i <= N; ++i)
            scanf("%lf%lf", &x[i], &y[i]);
        char ind[10];
        while(scanf("%s", ind) != EOF) {
            if(ind[0] == 'O') {
                int u;
                scanf("%d", &u);
                repair[u] = 1;
                for(int i = 1; i <= N; ++i)
                    if(u != i)
                        if(repair[i] && check(u, i))
                            Union(u, i);
            } else if(ind[0] == 'S') {
                int u, v;
                scanf("%d%d", &u, &v);
                u = Find(u), v = Find(v);
                if(u == v) printf("SUCCESS
    ");
                else printf("FAIL
    ");
            }
        }
        return 0;
    }
    View Code
    
    
  • 相关阅读:
    一个简单的jsp自定义标签
    js正则表达式学习
    java获取当前日期等以及时区
    java日期处理SimpleDateFormat等
    一个炫酷的导航菜单,模仿别人写的
    后台管理界面自己写,模仿,更新中...
    信息收集-主机综合扫描工具的使用
    ms10_046_shortcut_icon_dllloader漏洞利用和ettercap dns欺骗
    如何成为一名黑客
    msf常用命令
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12175624.html
Copyright © 2011-2022 走看看