zoukankan      html  css  js  c++  java
  • 05-树8 File Transfer(25 point(s)) 【并查集】

    05-树8 File Transfer(25 point(s))

    We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other?
    Input Specification:

    Each input file contains one test case. For each test case, the first line contains N (2≤N≤10​4​​), the total number of computers in a network. Each computer in the network is then represented by a positive integer between 1 and N. Then in the following lines, the input is given in the format:

    I c1 c2

    where I stands for inputting a connection between c1 and c2; or

    C c1 c2

    where C stands for checking if it is possible to transfer files between c1 and c2; or

    S

    where S stands for stopping this case.
    Output Specification:

    For each C case, print in one line the word “yes” or “no” if it is possible or impossible to transfer files between c1 and c2, respectively. At the end of each case, print in one line “The network is connected.” if there is a path between any pair of computers; or “There are k components.” where k is the number of connected components in this network.
    Sample Input 1:

    5
    C 3 2
    I 3 2
    C 1 5
    I 4 5
    I 2 4
    C 3 5
    S

    Sample Output 1:

    no
    no
    yes
    There are 2 components.

    Sample Input 2:

    5
    C 3 2
    I 3 2
    C 1 5
    I 4 5
    I 2 4
    C 3 5
    I 1 3
    C 1 5
    S

    Sample Output 2:

    no
    no
    yes
    yes
    The network is connected.

    题意
    有N台电脑,C a b 表示将这两台电脑用网线连接起来 I a b 表示检查 这两台电脑之间是否连通
    最后还要输出连通块的个数

    AC代码

    #include <cstdio>
    #include <cstring>
    #include <ctype.h>
    #include <cstdlib>
    #include <cmath>
    #include <climits>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <map>
    #include <stack>
    #include <set>
    #include <numeric>
    #include <sstream>
    #include <iomanip>
    #include <limits>
    
    #define CLR(a) memset(a, 0, sizeof(a))
    #define pb push_back
    
    using namespace std;
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    typedef pair <int, int> pii;
    typedef pair <ll, ll> pll;
    typedef pair<string, int> psi;
    typedef pair<string, string> pss;
    
    const double PI = acos(-1);
    const double E = exp(1);
    const double eps = 1e-30;
    
    const int INF = 0x3f3f3f3f;
    const int maxn = 1e4 + 5;
    const int MOD = 1e9 + 7;
    
    int pre[maxn];
    
    int find(int x)
    {
        int r = x;
        while (r != pre[r])
            r = pre[r];
        int j = x, i;
        while (j != r)    //路径压缩 
        {
            i = pre[j];
            pre[j] = r;
            j = i;
        }
        return r;
    }
    
    void join(int x, int y)
    {
        int fx = find(x), fy = find(y);
        if (x != fy)
            pre[fx] = fy;
    }
    
    void init()
    {
        for (int i = 0; i < maxn; i++)
            pre[i] = i;
    }
    
    int main()
    {
        init();
        int n;
        scanf("%d", &n);
        char c;
        int a, b;
        while (true)
        {
            scanf(" %c", &c);
            if (c == 'C')
            {
                scanf("%d%d", &a, &b);
                if (find(a) == find(b))
                    printf("yes
    ");
                else
                    printf("no
    ");
            }
            else if (c == 'I')
            {
                scanf("%d%d", &a, &b);
                join(a, b);
            }
            else if (c == 'S')
            {
                map <int, int> m;
                for (int i = 1; i <= n; i++)
                    m[find(i)] ++;
                if (m.size() == 1)
                    printf("The network is connected.
    ");
                else
                    printf("There are %d components.
    ", m.size());
                break;
            }
        }
    }
    
  • 相关阅读:
    Unity资源打包学习笔记(一)、详解AssetBundle的流程
    Unity实现c#热更新方案探究(三)
    Unity实现c#热更新方案探究(二)
    Unity实现c#热更新方案探究(一)
    对C#热更新方案ILRuntime的探究
    Unity使用C++作为游戏逻辑脚本的研究(二)
    执行composer install/update 命令遇 "You are using an outdated version of Composer. Composer 2.0 is abo...
    php 安装xdebug进行调试(phpstorm)
    phpstudy如何设置Nginx伪静态
    JS正则表达式
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433147.html
Copyright © 2011-2022 走看看