PAT 5-8 File Transfer (25分)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT 5-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." wherek 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.

这题典型的是一道并查树的题。思路也非常简单,课件上也有现成。但是这道题直接用课本上的unite方法的话会超时。改进的办法有两个:1.unite时判断两个集合哪个集合的元素多,然后把元素少的那个集合并到大的里面,即直接把元素少的集合的根挂到另一个集合的根上。2.unite时,也是先判断哪个元素多,然后把元素少的那个集合的每一个元素都挂到元素多的那个集合的根上,这样做的话可以保证每个集合的高度只能是2,减少了find操作的耗时,但是会增加unite操作调整元素的时间。亲测用两种方法都能AC,下面给出两种方法的AC代码。
 1 #include<iostream>
 2 #include"stdio.h"
 3 using namespace std;
 4 
 5 int* a;
 6 
 7 void unite(int x1,int x2)
 8 {
 9     int root1 = x1-1;
10     while (a[root1]>=0) 
11         root1=a[root1];
12     int root2 = x2-1;
13     while (a[root2]>=0) 
14         root2=a[root2];
15 
16     if ((a[root1]) <= (a[root2])) //root1的集合较大
17     {
18         a[root1] += a[root2];    
19         a[root2] = root1;
20     }
21     else
22     {
23         a[root2] += a[root1];
24         a[root1] = root2;
25     }
26 }
27 
28 void judge(int x1,int x2)
29 {
30     int root1 = x1-1;
31     int root2 = x2-1;
32     while (a[root1]>=0) 
33         root1 = a[root1];
34     while (a[root2]>=0) 
35         root2 = a[root2];
36     if ( root1 == root2 )
37         printf("yes\n");
38     else
39         printf("no\n");
40 }
41 
42 int main()
43 {
44     int N=0;
45     cin >> N;
46     a = new int [N];
47 
48     for (int i=0;i<N;i++)
49     {
50         a[i] = -1;
51     }
52 
53     char operation=a;
54     int c1=0,c2=0;
55     
56     cin >> operation;
57     while (operation != S)
58     {
59         cin >> c1 >> c2;
60         if (operation == I)
61         {
62             unite(c1,c2);
63         }
64         else if (operation == C)
65         {
66             judge(c1,c2);
67         }
68         cin >> operation;
69     }
70     int component=0;
71     for (int i=0;i<N;i++)
72         if (a[i] < 0)
73             ++component;
74 
75     if (component == 1)
76         cout << "The network is connected." << endl;
77     else
78         cout << "There are " << component << " components." << endl;
79 
80     return 0;
81 }

 

 1 #include<iostream>
 2 #include<vector>
 3 #include"stdio.h"
 4 using namespace std;
 5 
 6 struct PC
 7 {
 8     int data;
 9     int parent;
10     vector<int> children;//若为根节点,则此容器放的是整个集合的元素值
11 };
12 PC* a;
13 
14 int find(int x) //查找x属于哪个集合,返回根节点的下标
15 {
16     vector<int> vec;
17     for (;a[x-1].parent >=0; x=a[x-1].parent+1);
18     return x;
19 }
20 
21 void unite(int x1,int x2)
22 {
23     int root1 = find(x1);
24     int root2 = find(x2);
25     if (-(a[root1-1].parent) >= -(a[root2-1].parent)) //root1的集合较大
26     {
27         a[root1-1].parent += a[root2-1].parent;
28         while (!a[root2-1].children.empty())
29         {
30             a[root1-1].children.push_back(a[root2-1].children.back());
31             a[ a[root2-1].children.back()-1 ].parent = root1-1;
32             a[root2-1].children.pop_back();
33         }
34     }
35     else
36     {
37         a[root2-1].parent += a[root1-1].parent;
38         while (!a[root1-1].children.empty())
39         {
40             a[root2-1].children.push_back(a[root1-1].children.back());
41             a[ a[root1-1].children.back()-1 ].parent = root2-1;
42             a[root1-1].children.pop_back();
43         }
44     }
45 }
46 
47 int main()
48 {
49     int N=0;
50     scanf("%d",&N);
51     a = new PC[N];
52     for (int i=0;i<N;i++)
53     {
54         a[i].data = i+1;
55         a[i].parent = -1;
56         a[i].children.push_back(i+1);
57     }
58 
59     char operation=a,temp;
60     int c1=0,c2=0;
61     
62     scanf(" %c",&operation);
63     while (operation != S)
64     {
65         scanf("%d %d",&c1,&c2);
66         if (operation == C)
67         {
68             if ( find(c1) == find(c2) )
69                 printf("yes\n");
70             else
71                 printf("no\n");
72         }
73         if (operation == I)
74         {
75             unite(c1,c2);
76         }
77         scanf(" %c",&operation);
78     }
79     int component=0;
80     for (int i=0;i<N;i++)
81     {
82         if (a[i].parent < 0)
83             ++component;
84     }
85     if (component == 1)
86     {
87         cout << "The network is connected." << endl;
88     }
89     else
90     {
91         cout << "There are " << component << " components." << endl;
92     }
93     return 0;
94 }

 



以上是关于PAT 5-8 File Transfer (25分)的主要内容,如果未能解决你的问题,请参考以下文章

05-树8 File Transfer (25分)

PAT 1005 继续(3n+1)猜想 (25)

PAT——1005. 继续(3n+1)猜想 (25)

PAT 乙级 1005. 继续(3n+1)猜想 (25)

PAT Basic 1005 继续(3n+1)猜想 (25 分)

PAT 1005 继续(3n+1)猜想