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;
            }
        }
    }
    
  • 相关阅读:
    对比.NET PetShop和Duwamish来探讨Ado.NET的数据库编程模式
    找到了一个动态加载web用户自定义控件的问题,不知道算不算是微软的bug
    今天碰到了一个取 REMOTE_USER 的问题
    解决震荡波补丁引起的oracle不能启动
    有几个gmail的邀请,需要的留个言吧。
    一种插入记录的方式,撇开效率,看看对不对
    Regex 类介绍
    Page执行周期
    一段xml deserialize解释
    突然产生的一个想法,写一个基类,用来完成对LoadControl后续操作进行管理
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433147.html
Copyright © 2011-2022 走看看