浙大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 ...
随机推荐
- php单引号和双引号的区别与用法
php里的单引号把内容当成纯文本,不会经过服务器翻译.而双引号则与此相反.里面的内容会经过服务器处理(process). 举个简单的例子: $foo="data"; echo ...
- Yii render和renderPartial的区别
以下由我们在信易网络公司开发项目的时候终结出的一些经验 在进行页面输出渲染的时候. 1.render 输出父模板的内容,将渲染的内容,嵌入父模板.|2.renderPartial 则不输出父模板的内容 ...
- 安装Cocoa 新的依赖管理工具Carthage
Cocoa的依赖管理器,我们已经有了CocoaPods,非常好用,那么为什么还要创建这样一个项目呢?本文翻译自Carthage的Github的README.md,带大家来了解一下这个工具有何不同之处. ...
- 在虚拟机的linux中利用VMware Tools实现与windows共享文件
很多人都知道安装"VMware Tools"可以实现与windows共享,但是其实它的功能远不止此.安装了"VMware Tools"后,虚拟机的网络. ...
- KVO - 键值观察
[基本概念] 键值观察是一种使对象获取其他对象的特定属性变化的通知机制.控制器层的绑定技术就是严重依赖键值观察获得模型层和控制器层的变化通知的.对于不依赖控制器层类的应用程序,键值观察提供了一种简化的 ...
- Hbase案例分析(一)
Hbase应用场景: 1 随机读或者写 2 大数据上的高并发操作,比如每秒对PB级数据进行上千次操作.(查询,删除等操作) 3 读写均是非常简单的操作,比如没有join操作 Hbase Schema设 ...
- css3加载中
.loader { margin: 6em auto; font-size: 10px; position: relative; text-indent: -9999em; border-top: 1 ...
- bzoj 2402: 陶陶的难题II 二分答案维护凸包
2402: 陶陶的难题II Time Limit: 40 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 68 Solved: 45[Submi ...
- Android java程序获取assets资产文件
AssetManager assetManager=this.getAssets(); inputStream = assetManager.open("test.xml");
- Linq Distinct List 去重复
//调用 return producePlantlst.Distinct(new item_collection_DistinctBy_item1()).ToList(); //方法 public c ...