zoukankan      html  css  js  c++  java
  • File Transfer

    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), 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.

    这道题挺基础的,考察并查集

     1 #include <cstdio> 
     2 #include <algorithm> 
     3 
     4 using namespace std;
     5 
     6 int father[10001];
     7 int isroot[10001];
     8 
     9 int findfather(int X)
    10 {
    11     int R;
    12     if(X!=father[X])
    13         R=findfather(father[X]);
    14     else R=X;
    15     father[X]=R;
    16     return R;
    17 }
    18 
    19 int main()
    20 {
    21     int n;
    22     scanf("%d",&n);
    23     getchar();
    24     for(int i=1;i<=n;i++)
    25     {
    26         father[i]=i;
    27     }
    28     char C;
    29     while(scanf("%c",&C)&&C!='S')
    30     {
    31         int a,b,FA,FB;
    32         scanf("%d %d",&a,&b);
    33         if(C=='I')
    34         {
    35             FA=findfather(a);
    36             FB=findfather(b);
    37             if(FA!=FB)
    38                 father[FA]=FB;
    39         }
    40         if(C=='C')
    41         {
    42             FA=findfather(a);
    43             FB=findfather(b);
    44             if(FA==FB)
    45                 printf("yes
    ");
    46             else
    47                 printf("no
    ");
    48         }
    49     }
    50     for(int i=1;i<=n;i++)
    51     {
    52         if(i==findfather(i))
    53             isroot[i]=1;
    54     }
    55     int num=0;
    56     for(int i=1;i<=n;i++)
    57     {
    58         if(isroot[i])
    59             num++;
    60     }
    61     if(num!=1)
    62         printf("There are %d components.
    ",num);
    63     else 
    64         printf("The network is connected.
    ");
    65     return 0;
    66 } 

  • 相关阅读:
    .NET微服务调查结果
    发布基于Orchard Core的友浩达科技官网
    Followme Devops实践之路
    积极参与开源项目,促进.NET Core生态社区发展
    Service Fabric 与 Ocelot 集成
    “.Net 社区大会”(dotnetConf) 2018 Day 1 主题演讲
    Project file is incomplete. Expected imports are missing 错误解决方案
    稳定工作和创业之间的抉择
    回顾4180天在腾讯使用C#的历程,开启新的征途
    ML-Framework:ML.NET 0.3 带来新组件
  • 原文地址:https://www.cnblogs.com/jiamian/p/10764874.html
Copyright © 2011-2022 走看看