【学到的东西】
1)集合的简化表示:不用结构体数据,直接用数组
2)简单实现->按秩归并:①Union()中单纯将S[root1] = S[root2],会导致集合(树)越来越长,如果访问最底部的元素,找根结点的时候会递归很多层,对n个输入执行FindRoot()最坏情况时间复杂度O(n^2)。②【按秩归并】更进一步,可将两棵树比较高度,将矮的合并到高的,或者规模小的合并到规模大的,这样高度不会太大。最坏情况下树的高度为O(logN),然后最坏情况下(每次访问都最底层元素)时间复杂度O(N logN)。③最终的方法是在递归寻找根结点的同时,将路径上的S[x]改为最终根结点,这样之后再访问该路径上的元素,就很快了。ps.由于使用了③方法,如果按照高度归并,则在进行③时树的高度会改变,应同时改变S[],麻烦一点,所以使用按规模归并。
3)关于带路径压缩查找与按秩归并交替进行的时间复杂度(Ackerman函数)
Ackerman函数增长非常快,A(2,4)=2^65536,因此log*N的结果是一个很小的数,即使N很大很大,在一般情况下log*N都是<=4的。
题目
7-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:
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.
#include<iostream>
#include<cstdio>
using namespace std;
#define MaxSize 10000
typedef int ElementType; //用下标表示int数据
typedef int SetName; //集合名字(数组下标)
typedef ElementType SetType[MaxSize];
void InputCo(SetType S);
void CheckCo(SetType S);
void CheckNet(SetType S, int n);
SetType S;
int main() {
int n, i;
char op;
cin >> n;
for (i = 0; i < n; ++i) { //并查集初始化
S[i] = -1;
}
do {
cin >> op;
switch (op) {
case 'I':InputCo(S); break;
case 'C':CheckCo(S); break;
case 'S':CheckNet(S, n); break;
}
} while (op != 'S');
return 0;
}
void printS(SetType S, int n) { //用来查看建立连接之后的S[]
int i;
for (i = 0; i < n; ++i) {
cout << S[i] << " ";
}
cout << endl;
}
SetName FindRoot(SetType S, ElementType x) {
if (S[x] < 0) return x; //x自己就是根
return S[x] = FindRoot(S, S[x]); //查找的时候,顺便将路径上的每一个S[x]设置为最终根结点
}
void Union(SetType S, SetName root1, SetName root2) {
if (S[root2] > S[root1]) { //如果root1规模更大,S[]非负数表示父结点(非根结点),用负数表示该集合的规模(根结点)
S[root1] += S[root2];
S[root2] = root1;
}
else {
S[root2] += S[root1];
S[root1] = root2;
}
}
void InputCo(SetType S) {
ElementType u, v;
SetName root1, root2;
cin >> u >> v;
root1 = FindRoot(S, u - 1); //在S中查找下标为u-1(值为u)的元素的父结点(值从1开始,下标从0开始)
root2 = FindRoot(S, v - 1);
if (root1 != root2) Union(S, root1, root2);
//printS(S, 5);
}
void CheckCo(SetType S) {
ElementType u, v;
SetName root1, root2;
cin >> u >> v;
root1 = FindRoot(S, u - 1);
root2 = FindRoot(S, v - 1);
if (root1 == root2) cout << "yes" << endl;
else cout << "no" << endl;
}
void CheckNet(SetType S, int n) { //判断整个网络是否连接起来:遍历S[],看是不是只有一个负数(根结点)
int i, count = 0;
for (i = 0; i < n; ++i) {
if (S[i] < 0) count++;
}
if (count == 1) cout << "The network is connected." << endl;
else printf("There are %d components.\n", count);
}