浙大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生成制作验证码
看完就会,不会你打我,话不多说.开搞(人狠话不多) 1.0 首先先看代码 <?php header("Content-Type:text/html;Charset=UTF-8" ...
- javascript进阶——测试和打包分发
建立一个面向对象的好的代码基础后,为了达到代码重用的目的,通过调试使用适当的测试用例进行测试尤为必要,之后就是打包分发的主题. 一.调试与测试 1.调试 Firebug:包含了错误控制台.调试器.DO ...
- 解决android调用IIS Express中的WCF服务时,出现错误400问题
IIS Express仅支持localhost主机名地址访问. 找到IIS Express Config文件下的 applicationhost.confi 修改配置 再来调试android应用, ...
- ubuntu导出文件
ye@aliyun:python$ ./deploy.sh backup static-rw-r--r-- 1 ye ye 174K 2014-03-22 10:36 ./backup/fbz_sta ...
- 写一个TT模板自动生成spring.net下面的配置文件。
这个是目标. 然后想着就怎么开始 1.
- 双数组Trie树 (Double-array Trie) 及其应用
双数组Trie树(Double-array Trie, DAT)是由三个日本人提出的一种Trie树的高效实现 [1],兼顾了查询效率与空间存储.Ansj便是用DAT(虽然作者宣称是三数组Trie树,但 ...
- share my tools With Xcode
1.让Xcode的控制台支持LLDB类型的打印 在Xcode断点调试的时候, 在控制台输入 po self.view.frame 或者 po id 类型的时候就死翘翘了. 进入正题: 安装LLDB调试 ...
- 导航 -MapKit - 获取路线信息绘制导航路线
#import "PPViewController.h" #import <MapKit/MapKit.h> #import "PPAnnotation.h& ...
- C#中Socket编程解决应用程序直接的通信
using System;using System.Collections.Generic;using System.Linq;using System.Text; using System.Net; ...
- HTML5 知识点
HTML5 知识点 (1)语义化标记 <header>,<footer>,<nav>,<article>,<section> ...