浙大PTA - - File Transfer
题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21732
#include "iostream"
#include "algorithm"
using namespace std;
int father[10001], n;
/* 合并思路:
1.要将小子树合并到大子树上 反过来合并 树会退化成单链表 导致查询时间变为线性时间 从而导致超时
2. 可以采用按节点数大小合并 也可以按树高进行归并(树高变化当且仅当进行归并的2个树高度相同)
3. 本题采用按节点数进行归并
4. 路径压缩 (防止测试数据总是从叶子节点查询 导致O(n*log(n))的查询时间复杂度)
*/
void Init() {
for (int i = 1; i <= n; i++)
father[i] = -1; /* 初始化为当前树的节点数的相反数 */
}
int Find(int x) { /* 查询根节点 */
if (father[x] <= -1)
return x;
else
return father[x] = Find(father[x]); /* 路径压缩 */
} void Union(int x, int y) {
x = Find(x);
y = Find(y);
if(father[x] < father[y]){ /* 按节点数大小进行归并 */
father[x] += father[y];
father[y] = x;
n--; /* 2个集合归并 总集合数减一*/
}
else {
father[y] += father[x];
father[x] = y;
n--;
}
}
int main() {
char ch;
int i, j;
cin >> n;
Init();
bool flag = true;
while (cin >> ch) { //cout << ch << endl;
if (ch == 'S') {
if (n == 1)
cout << "The network is connected." << endl;
else
cout << "There are " << n << " components." << endl;
break;
}
cin >> i >> j;
switch (ch) {
case 'C':
if (Find(i) == Find(j)) {
cout << "yes" << endl;
}
else {
cout << "no" << endl;
}
break;
case 'I':Union(i, j); break;
}
}
return 0;
}
浙大PTA - - File Transfer的更多相关文章
- File Transfer
本博客的代码的思想和图片参考:好大学慕课浙江大学陈越老师.何钦铭老师的<数据结构> 代码的测试工具PTA File Transfer 1 Question 2 Explain First, ...
- PTA 05-树8 File Transfer (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/670 5-8 File Transfer (25分) We have a netwo ...
- PAT 5-8 File Transfer (25分)
We have a network of computers and a list of bi-directional connections. Each of these connections a ...
- 让 File Transfer Manager 在新版本WIndows上能用
最近研究.NET NATIVE,听说发布了第二个预览版,增加了X86支持,所以下,发现连接到的页面是:https://connect.microsoft.com/VisualStudio/Downlo ...
- PAT 05-树7 File Transfer
这次的题让我对选择不同数据结构所产生的结果惊呆了,一开始用的是结构来存储集合,课件上有现成的,而且我也是实在不太会,150ms的时间限制过不去,不得已,看到这题刚好可以用数组,结果7ms最多,有意思! ...
- 05-树8 File Transfer
并查集 简单并查集:输入N,代表有编号为1.2.3……N电脑.下标从1开始.初始化为-1.合并后根为负数,负数代表根,其绝对值代表个数. We have a network of computers ...
- Cordova Upload Images using File Transfer Plugin and .Net core WebAPI
In this article, I am going to explain ,"How to Upload Images to the server using Cordova File ...
- Trivial File Transfer Protocol (TFTP)
Assignment 2The Trivial File Transfer Protocol (TFTP) is an Internet software utility fortransferrin ...
- 05-树8 File Transfer (25 分)
We have a network of computers and a list of bi-directional connections. Each of these connections a ...
随机推荐
- H5小内容(三)
Canvas(画布) 基本内容 简单来说,HTML5提供的新元素<canvas> Canvas在HTML页面提供画布的功能 在画布中绘制各种图形 C ...
- Lucene4.9学习笔记——Lucene建立索引
基本上创建索引需要三个步骤: 1.创建索引库IndexWriter对象 2.根据文件创建文档Document 3.向索引库中写入文档内容 这其中主要涉及到了IndexWriter(索引的核心组件,用于 ...
- swift swizzle
SWIZZLE 由 王巍 (@ONEVCAT) 发布于 2015/09/30 Swizzle 是 Objective-C 运行时的黑魔法之一.我们可以通过 Swizzle 的手段,在运行时对某些方法的 ...
- Java 泛型快速排序 以sdut 1196为例
oj链接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1196 Java中,Arrays.so ...
- 使用ajax和history.pushState无刷新改变页面URL onpopstate(转)
Javascript代码 var htmlData1 = $.ajax( { url: "/getXXXResponse", async: false }).re ...
- 第 13 章 装饰模式【Decorator Pattern】
以下内容出自:<<24种设计模式介绍与6大设计原则>> Ladies and gentlemen,May I get your attention,Please?,Now I’ ...
- PS仿制图章
颜色总不对,和周围不太搭配,这时候把流量调小点,然后用渐变工具.自己实践所得.
- matlab中 hold on 与hold off的用法
matlab中 hold on 与hold off的用法 hold on 是当前轴及图形保持而不被刷新,准备接受此后将绘制 hold off 使当前轴及图形不在具备被刷新的性质 hold on 和ho ...
- 子查询解嵌套not in 无法展开改写
SQL> explain plan for select * from OPS$CZTEST1.SAVJ_ATOMJOURBAK where ((list_flag = '1' and prt_ ...
- PLSQL调用webservice
1. 用途简介 为什么要在Oracle中访问WebService?在系统实现中,有时会有直接在数据库端利用触发器.存储过程等方式进行数据传递.分发的业务,而其中可能会涉及一些业务逻辑,为了处理 ...