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;
            }
        }
    }
    
  • 相关阅读:
    让UILabel的文字顶部对齐
    常用的iOS开发或者优化的小工具
    AppStoreID--安装URL--应用更新URL--应用评分URL
    iOS 下载功能:断点下载(暂停和开始)(NSURLConnectionDataDelegate方法)
    iOS QLPreviewController(Quick Look)快速浏览jpg,PDF,world等
    如何不让UITableView滚动
    解析字典包含关键字比如ID,description等,MJExtension 框架 不能直接设置变量与其同名。
    今天犯了个小错误:_dataArray.count>1 和_dataArray.count>0搞混淆了
    获取当前的日期和时间-数码
    C/C++中的段错误(Segmentation fault)[转]
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433147.html
Copyright © 2011-2022 走看看