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 } 

  • 相关阅读:
    git将已存在的项目转换成git项目&托管git服务器
    联合索引
    MyISAM和InnoDb的关系
    NPOIHelper
    C# CRC各种转换
    appcloud 微信分享大图片
    C# 微信JSSDK 获取配置信息
    简易delegate委托
    GPS、谷歌、百度、高德坐标相互转换
    反射执行方法WINFROM
  • 原文地址:https://www.cnblogs.com/jiamian/p/10764874.html
Copyright © 2011-2022 走看看