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 }
				
时间: 2024-11-08 13:58:33

PAT 5-8 File Transfer (25分)的相关文章

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 Specificat

04-树5. File Transfer (25)

04-树5. File Transfer (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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

04-2. File Transfer (25)并查集

04-2. File Transfer (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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

pat04-树5. File Transfer (25)

04-树5. File Transfer (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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

PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)

1016 Phone Bills (25 分)   A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connec

PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)

1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers. Non-palindromic n

PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤) and C, wh

PAT 1036 Boys vs Girls (25 分)

1036 Boys vs Girls (25 分) This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students. Input Specification: Each input file contains one test case. Each case contai

PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)

1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if