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 } 

  • 相关阅读:
    自制编译器 青木峰郎 笔记 Ch5 基于JavaCC的解析器描述
    自制编译器 青木峰郎 笔记 Ch4 基于JavaCC的扫描器的描述
    自制编译器 青木峰郎 笔记 Ch3 词法分析的概要
    自制编译器 青木峰郎 笔记 Ch2 Cb和Cbc
    自制编译器 青木峰郎 笔记 Ch1 开始制作编译器
    POJ 1201 Intervals 差分约束,最短路,RE会报TLE 难度:1 差分约束背景知识需联想证明
    POJ 3276 The Cow Lexicon DP 难度: 0
    POJ 1276 Cash Machine DP 难度: 0
    Java大数练习
    HDU 4344-Mark the Rope-大数素因子分解
  • 原文地址:https://www.cnblogs.com/jiamian/p/10764874.html
Copyright © 2011-2022 走看看