pta 编程题13 File Transfer

其它pta数据结构编程题请参见:pta

这道题考察的是union-find并查集。

开始把数组中每个元素初始化为-1,代表没有父节点。为了使树更加平衡,可以让每一个连通分量的树根的负值代表这个连通分量包含的节点数,然后在union时把小的树并到大的树上。

另外在find操作时可以用递归的方式使查找路径上的所有节点的父节点都改为根节点,以实现路径压缩,在后续查找过程中会更快。

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4
 5 vector<int> computers;
 6
 7 void _union(int a, int b);
 8 int find(int a);
 9 void components();
10
11 int main()
12 {
13     int n, c1, c2;
14     char c;
15     cin >> n;
16     computers.resize(n + 1, -1);
17     do{
18         cin >> c;
19         switch (c)
20         {
21         case ‘I‘:
22             cin >> c1 >> c2;
23             _union(c1, c2);
24             break;
25         case ‘C‘:
26             cin >> c1 >> c2;
27             if (find(c1) == find(c2))
28                 cout << "yes" << endl;
29             else
30                 cout << "no" << endl;
31             break;
32         case ‘S‘:
33             components();
34             break;
35         }
36     } while (c != ‘S‘);
37     return 0;
38 }
39
40 void _union(int a, int b)
41 {
42     int root1 = find(a);
43     int root2 = find(b);
44     if (computers[root1] < computers[root2])
45     {
46         computers[root1] += computers[root2];//顺序不能...
47         computers[root2] = root1;//颠倒!
48     }
49     else {
50         computers[root2] += computers[root1];
51         computers[root1] = root2;
52     }
53 }
54 /*
55 int find(int a)
56 {
57     for (; computers[a] > 0; a = computers[a]);
58     return a;
59 }
60 */
61 int find(int a) //带路径压缩的查找
62 {
63     if (computers[a] < 0) return a;
64     else return computers[a] = find(computers[a]);
65 }
66
67 void components()
68 {
69     int count = 0;
70     for (int i = 1; i < computers.size(); i++)
71     {
72         if (computers[i] < 0) count++;
73     }
74     if (count > 1)
75         printf("There are %d components.\n", count);
76     else
77         printf("The network is connected.\n");
78 }

原文地址:https://www.cnblogs.com/lxc1910/p/8847521.html

时间: 2024-07-30 06:24:06

pta 编程题13 File Transfer的相关文章

pta 编程题15 列出连通集

其它pta数据结构编程题请参见:pta 题目 题目要求分别以深度优先搜索和广度优先搜索输出图的连通集. 广度优先搜索要用到队列,先回顾一下循环队列: 1 struct QNode { 2 int* Data; /* 存储元素的数组 */ 3 int Front, Rear; /* 队列的头.尾指针 */ 4 int MaxSize; /* 队列最大容量 */ 5 }; 6 typedef struct QNode *Queue; 7 8 Queue CreateQueue( int MaxSiz

pta 编程题21 公路村村通

其它pta数据结构编程题请参见:pta 题目 这道题考察最小生成树问题,用的是Prim算法. 1 #include <iostream> 2 using namespace std; 3 4 int N, M; 5 int** G; 6 void buildGraph(); 7 void deleteGraph(); 8 int prim(); 9 int findMinDist(int dist[]); 10 11 int main() 12 { 13 cin >> N >

pta 编程题14 Huffman Codes

题目 题目给出一组字母和每个字母的频数,因为哈夫曼编码不唯一,然后给出几组编码,因为哈夫曼编码不唯一,所以让你判断这些编码是否符合是哈夫曼编码的一种. 解题思路: 1.构造哈夫曼树,并求出总代价COST,即各个字母的频数乘以编码长度的和. 2.对于题目给出的每一组编码,判断是否符合哈夫曼编码,即这组编码是否为前缀码,同时代价cost是否等于计算出的哈夫曼树的代价COST. 判断一组编码是否为前缀码的方法: 将这些编码逐个的添加到哈夫曼树中,对于每一个编码字符串,字符串中的每一个字符也逐个扫描,如

PTA 5-8(English) File Transfer (25) - 并查集 - 数组实现

题目:http://pta.patest.cn/pta/test/16/exam/4/question/670 PTA - Data Structures and Algorithms (English) - 5-8 We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer t

基于TCP的TFTP(Trivial File Transfer Protocol,简单文件传输协议) 的c编程实现

我们或许都听到过,TFTP(Trivial File Transfer Protocol,简单文件传输协议)是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,提供不复杂.开销不大的文件传输服务. 本文就简单的叙述下tftp的小文件传输功能以及客户端对服务器的列表功能. 之前就一直很纳闷,我们经常在网上下载什么东西或者从别处传输一个文件,具体是怎么实现的呢?于是乎,翻查一些资料,加上自己对网络编程的逐步加深,所以功夫不负有心人,还算是大致的完成了下. 本例程实现的功能呢?

小米13笔试编程题 1

数组乘积(15分) 输入:一个长度为n的整数数组input 输出:一个长度为n的整数数组result,满足result[i] = input数组中除了input[i]之外所有数的乘积(假设不会溢出).比如输入:input = {2,3,4,5},输出result = {60,40,30,24} 程序时间和空间复杂度越小越好. C/C++: int *cal(int* input , int n); Java: int[] cal(int[] input); 方法1:算出数组所有元素乘积sum,再

小米13笔试编程题 4

朋友圈(25分) 假如已知有n个人和m对好友关系(存于数字r).如果两个人是直接或间接的好友(好友的好友的好友...),则认为他们属于同一个朋友圈,请写程序求出这n个人里一共有多少个朋友圈. 假如:n = 5 , m = 3 , r = {{1 , 2} , {2 , 3} , {4 , 5}},表示有5个人,1和2是好友,2和3是好友,4和5是好友,则1.2.3属于一个朋友圈,4.5属于另一个朋友圈,结果为2个朋友圈. 最后请分析所写代码的时间.空间复杂度.评分会参考代码的正确性和效率. C/

小米13笔试编程题 2

有一个数组(非递减),旋转了不知道多少个位,在该数组中找一个数的下标.写出代码(用C/C++或者java) 并分析时间空间复杂度,考虑效率(很重要). eg:数组 [6,7,1,2,3,4,4] 找3,返回4: 函数原型 C/C++: int find(int * a,int n,int count) count为a数组长度;n为要查找的数 Java: int find(int []a,int n) 方法:二分查找,插值查找,Fibonacci查找 二分查找如下: #include<iostre

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: