题目链接: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的更多相关文章

  1. File Transfer

    本博客的代码的思想和图片参考:好大学慕课浙江大学陈越老师.何钦铭老师的<数据结构> 代码的测试工具PTA File Transfer 1 Question 2 Explain First, ...

  2. 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 ...

  3. PAT 5-8 File Transfer (25分)

    We have a network of computers and a list of bi-directional connections. Each of these connections a ...

  4. 让 File Transfer Manager 在新版本WIndows上能用

    最近研究.NET NATIVE,听说发布了第二个预览版,增加了X86支持,所以下,发现连接到的页面是:https://connect.microsoft.com/VisualStudio/Downlo ...

  5. PAT 05-树7 File Transfer

    这次的题让我对选择不同数据结构所产生的结果惊呆了,一开始用的是结构来存储集合,课件上有现成的,而且我也是实在不太会,150ms的时间限制过不去,不得已,看到这题刚好可以用数组,结果7ms最多,有意思! ...

  6. 05-树8 File Transfer

    并查集 简单并查集:输入N,代表有编号为1.2.3……N电脑.下标从1开始.初始化为-1.合并后根为负数,负数代表根,其绝对值代表个数. We have a network of computers ...

  7. 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 ...

  8. Trivial File Transfer Protocol (TFTP)

    Assignment 2The Trivial File Transfer Protocol (TFTP) is an Internet software utility fortransferrin ...

  9. 05-树8 File Transfer (25 分)

    We have a network of computers and a list of bi-directional connections. Each of these connections a ...

随机推荐

  1. phpcms(1)phpcms V9 MVC模式 与 URL访问解析(转)

    [1]URL访问解析 观察访问网页时的网址,可以得出模块访问方法,如下示例: http://www.abcd.com.cn/phpcms/index.php?m=content&c=index ...

  2. 使用Yii框架中遇到的三个问题

    以下由我们在信易网络公司开发项目的时候终结出的一些经验 使用Yii框架中遇到的三个问题 1.main.php文件中欲引入全局变量的问题 还原一下此问题:在Yii框架中,main.php一般会作为整个应 ...

  3. Oracle课堂实验一“表的使用”代码。

    --创建本地管理表空间CustomerTBSCREATE TABLESPACE CustomerTBS         DATAFILE 'd:\Oracle11\product\11.2.0\ora ...

  4. linux服务器监控流量sh脚本

    服务器可能经常遇到服务器出带宽跑满,不知如何查询被哪个进程占用的情况,有一款开源的英文软件iftop功能比较强大可以查询相关信息,可能刚接触linux系统的朋友不太会使用,在此写了一个功能比较简单无需 ...

  5. mysql5.7版本无法启动服务问题

    cmd情况下进入mysql的bin目录后 输入命令:mysqld --initialize-insecure d:\mysql\bin

  6. activemq启动不起来,报错Address already in use: JVM_Bind

    之前莫名其妙的activemq怎么都启动不起来后来多方查询是因为widows 的ICS服务. 解决方案是,我的电脑上邮件,选择服务,然后在服务中找到Internet Connection Sharin ...

  7. 【技术贴】解决 myeclipse打不开报错an error has occurred, see .

    方法1.右键选中快捷方式属性选项,在快捷方式页,目标一项最后加上-clean选项,如C:\MyEclipse6\eclipse.exe -clean. 然后重新启动一下MyEclipse. 方法2. ...

  8. C#中数据库连接的配置文件

    在C#2010中,如何保存和访问数据库的连接字符串呢? 在Winform下要新增App.config文件,在Asp.net下要新增web.config文件. 1.打开配置文件添加相关代码后如下即可: ...

  9. uva 12124 - Assemble

    最大值最小的题: 直接用二分,比较简单: 不过我的二分老是不用好.有时间总结一下! #include<cstdio> #include<map> #include<vec ...

  10. phpStorm 配置关联php手册

    phpStorm 配置关联php手册 pasting php开发中我尝试过很多个编辑器,但用的最多的是phpStorm ,但一直因为英文太烂,很多phpStorm功能,都没用过.. 最近发现有些编辑器 ...