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≤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.
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.
Note1
- 思路:并查集的查和并, data域用数组下标表示,parent为数组中的内容, -1代表是根,
- 查:若不是根就一直查,返回根
- 并: 直接把两个合并,(有问题,不能保证两个集合归并,只能保证一个集合和以另一个元素为根的子集的归并)
- vector 全局变量失败?
- 退出时还在等两个元素输入,debug了好长时间
- 结果 1个超时点 + 1个错误点
Code1
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int s[100010];
int Find(int s[], int x) {
for(; s[x - 1] >= 0; x = s[x - 1]);
return x;
}
void Union (int s[], int r1, int r2) {
s[r2 - 1] = r1;
}
int main() {
int num;
cin >> num;
for(int i = 0; i < num; i++) s[i] = -1;
while(1) {
char temp;
int p1, p2;
cin >> temp;
if(temp == 'C') {
cin >> p1 >> p2;
if(Find(s, p1) == Find(s, p2)) cout << "yes" << endl;
else cout << "no" << endl;
}
else if(temp == 'I'){
cin >> p1 >> p2;
Union(s, p1, p2);
}
else if(temp == 'S') {
int cnt = 0;
for(int j = 0; j < num; j++){
if(s[j] == -1) cnt++;
}
if(cnt == 1) cout << "The network is connected.";
else cout << "There are " << cnt <<" components.";
break;
}
}
return 0;
}
Note2
- 路径压缩: 在每次find的时候把树的高度降低,使之后的find速度更快
- 按质归并: 将矮的树归并到高的树上(原因:新的树的高度认为原来高树的高度)
- 并: 改为两个根节点的并
Code2
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int s[100010];
int Find(int s[], int x) {
if(s[x] < 0)
return x;
else
return s[x] = Find(s, s[x] );
}
void Union (int s[], int r1, int r2) {
if(s[r2] < s[r1]){
s[r1] = r2;
}
else{
if(s[r1] == s[r2]) s[r1]--;
s[r2] = r1;
}
}
int main() {
int num;
cin >> num;
for(int i = 0; i < num; i++) s[i] = -1;
while(1) {
char temp;
int p1, p2;
cin >> temp;
if(temp == 'C') {
cin >> p1 >> p2;
p1 = Find(s, p1 - 1);
p2 = Find(s, p2 - 1);
if(p1 == p2) cout << "yes" << endl;
else cout << "no" << endl;
}
else if(temp == 'I'){
cin >> p1 >> p2;
p1 = Find(s, p1 - 1);
p2 = Find(s, p2 - 1);
Union(s, p1, p2);
}
else if(temp == 'S') {
int cnt = 0;
for(int j = 0; j < num; j++){
if(s[j] <= -1) cnt++;
}
if(cnt == 1) cout << "The network is connected.";
else cout << "There are " << cnt <<" components.";
break;
}
}
return 0;
}