• 05-树8 File Transfer(并查集)


    05-树8 File Transfer

    分数 25

    作者 陈越

    单位 浙江大学

    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≤104), 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:

    1. 5
    2. C 3 2
    3. I 3 2
    4. C 1 5
    5. I 4 5
    6. I 2 4
    7. C 3 5
    8. S

    Sample Output 1:

    1. no
    2. no
    3. yes
    4. There are 2 components.

    Sample Input 2:

    1. 5
    2. C 3 2
    3. I 3 2
    4. C 1 5
    5. I 4 5
    6. I 2 4
    7. C 3 5
    8. I 1 3
    9. C 1 5
    10. S

    Sample Output 2:

    1. no
    2. no
    3. yes
    4. yes
    5. The network is connected.

    1)并查集:(按秩合并,路径压缩);

    1. /**
    2. 1)并查集:(按秩合并,路径压缩);
    3. */
    4. #include
    5. #include
    6. using namespace std;
    7. const int maxn=1e4+10;
    8. int fath[maxn]={0};
    9. int Find(int x);
    10. void Union(int x,int y);
    11. int main()
    12. {
    13. int n;
    14. cin >> n;
    15. string op;
    16. int a,b;
    17. while(cin >> op,op!="S")
    18. {
    19. if(op=="C")
    20. {
    21. cin >> a >> b;
    22. a=Find(a),b=Find(b);
    23. if(a==b)
    24. cout << "yes\n";
    25. else
    26. cout << "no\n";
    27. }
    28. else
    29. {
    30. cin >> a >>b;
    31. Union(a,b);
    32. }
    33. }
    34. int flag=0;
    35. for(int i=1;i<=n;++i)
    36. if(fath[i]==0)
    37. ++flag;
    38. if(flag==1)
    39. cout << "The network is connected.\n";
    40. else
    41. printf("There are %d components.",flag);
    42. return 0;
    43. }
    44. int Find(int x)
    45. {
    46. if(fath[x]==0)
    47. return x;
    48. else
    49. return fath[x]=Find(fath[x]);
    50. }
    51. void Union(int x,int y)
    52. {
    53. x=Find(x);
    54. y=Find(y);
    55. if(x!=y)
    56. fath[x]=y;
    57. }

    2)我下次再遇到操作符与运算符在一行的时候,我一定分开写,不用string一起读入,
        后面再取运算符,两次都是犯这种错误,样例给的运算符是个位数,就想当然的把所有
        数字都想成了单数,我真是……
        直接用int 输入发便许多。

    1. /**
    2. 2)我下次再遇到操作符与运算符在一行的时候,我一定分开写,不用string一起读入,
    3. 后面再取运算符,两次都是犯这种错误,样例给的运算符是个位数,就想当然的把所有
    4. 数字都想成了单数,我真是……
    5. 直接用int 输入发便许多。
    6. */
    7. /**
    8. #include
    9. #include
    10. #include
    11. using namespace std;
    12. const int maxn=1e4+10;
    13. int fath[maxn]={0};
    14. int Find(int x);
    15. void Union(int x,int y);
    16. int main()
    17. {
    18. memset(fath,-1,sizeof(fath));
    19. int n;
    20. cin >> n;
    21. getchar();
    22. string str;
    23. while(getline(cin,str),str!="S")
    24. {
    25. if(str[0]=='C')
    26. {
    27. int a=0,b=0;
    28. int index=2; //我下次再遇到操作符与运算符在一行的时候,我一定分开写,不用string一起读入,后面再取运算符,两次都是犯这种错误,样例给的运算符是个位数,就想当然的把所有数字都想成了单数,我真是……
    29. for(index=2;indexindex]!=' ';++index)
    30. a=a*10+str[index]-'0';
    31. for(++index;indexindex)
    32. b=b*10+str[index]-'0';
    33. a=Find(a),b=Find(b);
    34. if(a==b)
    35. cout << "yes\n";
    36. else
    37. cout << "no\n";
    38. }
    39. else
    40. {
    41. int a=0,b=0;
    42. int index=2;
    43. for(index=2;indexindex]!=' ';++index)
    44. a=a*10+str[index]-'0';
    45. for(++index;indexindex)
    46. b=b*10+str[index]-'0';
    47. Union(a,b);
    48. }
    49. }
    50. int flag=0;
    51. for(int i=1;i<=n;++i)
    52. if(fath[i]<0)
    53. ++flag;
    54. if(flag==1)
    55. cout << "The network is connected.\n";
    56. else
    57. printf("There are %d components.",flag);
    58. return 0;
    59. }
    60. int Find(int x)
    61. {
    62. if(fath[x]<0)
    63. return x;
    64. else
    65. return fath[x]=Find(fath[x]);
    66. }
    67. void Union(int x,int y)
    68. {
    69. x=Find(x);
    70. y=Find(y);
    71. if(x!=y)
    72. {
    73. if(fath[x]y])
    74. {
    75. fath[x]+=fath[y];
    76. fath[y]=x;
    77. }
    78. else
    79. {
    80. fath[y]+=fath[x];
    81. fath[x]=y;
    82. }
    83. }
    84. }
    85. */

  • 相关阅读:
    基于Dijkstra和A*算法的机器人路径规划(Matlab代码实现)
    一张图搞定英文星期、月份、季节总也搞不定的星期,月份,季节,一张图搞定,还有必用的常见搭配,再也不担心用错介词了~
    ElasticSearch7.3学习(六)----文档(document)内部机制详解
    径向基函数RBF神经网络相关函数设置
    43、Flink之Hive 读写及详细验证示例
    低压配电室电力安全解决方案
    VSCode 最好的 Python 扩展,可大大提升你的生产力
    TypeScript-04基础知识之类型操纵、类
    java基础-集合
    C# 学习之路(C# 的概念)
  • 原文地址:https://blog.csdn.net/qq_51825761/article/details/126206868