关于并查集的操作,以及并查集算法的优化,按秩归并和路径压缩。
https://pintia.cn/problem-sets/1077214780527620096/problems/1111280682067111937
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≤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.1
2
3
4
5
6
7
8
9Sample Input 1:
5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
S
1 | Sample Output 1: |
1 | Sample Input 2: |
1 | Sample Output 2: |
并没有分析,陈越姥姥在小白专场里讲的非常好了,基础的部分看一下何老师的集合回顾一下就ok。
详细注释见代码。
代码
1 |
|