并查集

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 kcomponents.” where k is the number of connected components in this network.

Sample Input 1:

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

Sample Output 1:

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

Sample Input 2:

1
2
3
4
5
6
7
8
9
10
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:

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

一道典型的并查集的题目,刚开始没想太多,直接上了结构数组,结果当然超时了,简直惨不忍睹。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

using namespace std;
const int maxSize=10005;
int Size=0;
struct
{
int data;
int parent;
};
int Find(int x,US *g)
{
int i;
for(i=0;i<Size && g[i].data!=x;i++);
if(i>=Size) return -1;
for(;g[i].parent>=0;i=g[i].parent);
return i;
}
void check(int a,int b,US *g)
{
if(Find(a,g)>=0 && Find(b,g)>=0
&& Find(a,g)==Find(b,g)) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
int Insert(int a,int b,US *g)
{
if(Find(a,g)>=0 && Find(b,g)<0 || Find(a,g)<0 && Find(b,g)>=0){
int root=max(Find(a,g),Find(b,g));
root==Find(a,g) ? g[Size].data=b : g[Size].data=a;
g[Size].parent=root;
g[root].data--;
return Size+1;
}
if(Find(a,g)>=0 && Find(b,g)>=0){
if(g[Find(a,g)].data<g[Find(b,g)].data){
g[Find(a,g)].parent+=g[Find(b,g)].parent;
g[Find(b,g)].parent=Find(a,g);
}
else{
g[Find(b,g)].parent+=g[Find(a,g)].parent;
g[Find(a,g)].parent=Find(b,g);
}
return Size;
}
g[Size].data=a;
g[Size].parent=-2;
g[Size+1].data=b;
g[Size+1].parent=Size;
return Size+2;
}
int main() {
int n,a,b,cnt=0;
char c;
US *g=new US;
cin>>n;
while(cin>>c && c!='S' && cin>>a>>b){
if(c=='I'){
Size=Insert(a,b,g);
}
else check(a,b,g);
}
for(int i=0;i<Size;i++){
if(g[i].parent<0) cnt++;
}
if(cnt==1 && n==Size) cout<<"The network is connected.";
else cout<<"There are "<<cnt+n-Size<<" components.";
return 0;
}

看了老师的提示,由于编号为1—N,可以直接用数组表示并查集,利用按秩归并进行优化,即将小集合并到大集合上,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

using namespace std;
const int maxSize=10005;
int Find(int x,int* US)
{
for(;US[x]>=0;x=US[x]);
return x;
}
void Union(int root1,int root2,int* US)
{
US[root1]+=US[root2];
US[root2]=root1;
}
void check(int a,int b,int* US)
{
if(Find(a,US)==Find(b,US)) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
void Insert(int a,int b,int* US)
{
int root1=Find(a,US),root2=Find(b,US);
US[root1]<US[root2] ? Union(root1,root2,US):Union(root2,root1,US);
}
int main() {
int n,a,b,cnt=0;
char c;
cin>>n;
int US[n];
for(int i=0;i<n;i++){US[i]=-1;};
while(cin>>c && c!='S' && cin>>a>>b){
if(c=='I'){
Insert(a-1,b-1,US);
}
else check(a-1,b-1,US);
}
for(int i=0;i<n;i++){
if(US[i]<0) cnt++;
}
if(cnt==1) cout<<"The network is connected.";
else cout<<"There are "<<cnt<<" components.";
return 0;
}