05-树8 File Transfer
并查集
简单并查集:输入N,代表有编号为1、2、3……N电脑。下标从1开始。初始化为-1。合并后根为负数,负数代表根,其绝对值代表个数。
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:
Each input file contains one test case. For each test case, the first line contains N(2 <= N <= 10^4), the total number of computers in a network. Each computer in the network is then represented by a positive integer between 1 and N. Then in the following lines, the input is given in the format:
I c1 c2
where I
stands for inputting a connection between c1
and c2
; or
C c1 c2
where C
stands for checking if it is possible to transfer files between c1
and c2
; or
S
where S
stands for stopping this case.
Output Specification:
For each C
case, print in one line the word "yes" or "no" if it is possible or impossible to transfer files between c1
and c2
, respectively. At the end of each case, print in one line "The network is connected." if there is a path between any pair of computers; or "There are k
components." where k
is the number of connected components in this network.
Sample Input 1:
5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
S
Sample Output 1:
no
no
yes
There are 2 components.
Sample Input 2:
5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
I 1 3
C 1 5
S
Sample Output 2:
no
no
yes
yes
The network is connected.
#include <stdio.h> #define MaxN 10001 /* 集合最大元素个数 */ typedef int ElementType; /* 默认元素可以用非负正数表示 */
typedef int SetName; /* 默认用根节点下标作为集合名称 */
ElementType SetType[MaxN]; /* 假设集合元素下标从1开始 */ void Union( ElementType S[], SetName Root1, SetName Root2 );
SetName Find( ElementType S[], ElementType X ); int main()
{
int N;
int computerA,computerB;
scanf("%d",&N);
for(int i = ; i < N+; i++)
SetType[i] = -;
char operation;
scanf("%c",&operation);
while(operation != 'S') {
if(operation == 'I') { //inputting a connection
scanf("%d %d",&computerA, &computerB);
Union(SetType,Find(SetType,computerA), Find(SetType,computerB));
}else if(operation == 'C') { //check
scanf("%d %d",&computerA, &computerB);
if(Find(SetType,computerA) == Find(SetType,computerB)) { //是否是同一个根
printf("yes\n");
}else {
printf("no\n");
}
}
scanf("%c",&operation);
}
int components = ;
for(int i = ; i < N+; i++) {
if(SetType[i] < )
components++;
}
if(components == )
printf("The network is connected.\n");
else
printf("There are %d components.\n",components);
return ;
}
/* 这里默认Root1和Root2是不同集合的根结点 */
void Union( ElementType S[], SetName Root1, SetName Root2 )
{
/* 保证小集合并入大集合 */
if ( S[Root2] < S[Root1] ) { /* 如果集合2比较大 */
S[Root2] += S[Root1]; /* 集合1并入集合2 */
S[Root1] = Root2;
}
else { /* 如果集合1比较大 */
S[Root1] += S[Root2]; /* 集合2并入集合1 */
S[Root2] = Root1;
}
} SetName Find( ElementType S[], ElementType X )
{ /* 默认集合元素全部初始化为-1 */
if ( S[X] < ) /* 找到集合的根 */
return X;
else
return S[X] = Find( S, S[X] ); /* 路径压缩 */
}
05-树8 File Transfer的更多相关文章
- 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
本博客的代码的思想和图片参考:好大学慕课浙江大学陈越老师.何钦铭老师的<数据结构> 代码的测试工具PTA File Transfer 1 Question 2 Explain First, ...
- File Transfer(并查集)
题目大意:将多个电脑通过网线连接起来,不断查询2台电脑之间是否连通. 问题来源:中国大学mooc 05-树8 File Transfer (25 分) We have a network of com ...
- 让 File Transfer Manager 在新版本WIndows上能用
最近研究.NET NATIVE,听说发布了第二个预览版,增加了X86支持,所以下,发现连接到的页面是:https://connect.microsoft.com/VisualStudio/Downlo ...
- PAT 05-树7 File Transfer
这次的题让我对选择不同数据结构所产生的结果惊呆了,一开始用的是结构来存储集合,课件上有现成的,而且我也是实在不太会,150ms的时间限制过不去,不得已,看到这题刚好可以用数组,结果7ms最多,有意思! ...
- 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 ...
- SSH File Transfer遇到错误"too many authentication failures for root".A protocol error was detected......
在SSH Secure Shell 连接Linux centos的时候,遇到F-Secure SSH File Transfer错误"too many authentication fai ...
随机推荐
- QTEmbedded VCN实现
1 在QT在第三方插件里加入VNC 进入\src\plugins\gfxdrivers\vnc,qmake -------make--------- make install 把目标文件夹gfxdri ...
- Hive基础之自定义封装hivefile命令
存在的问题:当把hql写到shell中,不方便阅读:但把hql写到文件中,又传递不了参数:怎么办呢? 自定义hivefile 执行方式形如: 第一个参数为要执行的hql文件,后续的参数为要替换的key ...
- android menu 开发
menu 分类: 选项菜单(OptionsMenu) 上下文菜单(ContextMenu) 子菜单(SubMenu) 弹出菜单(Popup) 首先说 选项菜单(OptionsMenu) 一.方法介 ...
- easyui combo下拉框多选框
按照自己的方式,先晒下效果图: 选一个值,那么就在input里面显示一个,去掉勾选,那么input就会少一个 其实做法很简单,今天就是快下班了,闲着没事加篇博客而已,下面带上代码. 1.页面的展示,i ...
- [原创]大连sap vt 实习生面试经历
从决定参加这次面试开始,求职生涯就算是开始了,虽然失败了,但也是亲身体会到了面试的感觉,这次经历也作为第一篇博客. 大连sap的职能是sap的全球技术支持中心和解决方案提供中心.VT项目提供的岗位是技 ...
- panguan(判官):一个自研的任务执行引擎的工程实践
来某厂接近半年了,几乎没写过C++代码,说实话还真的有点手生.最近刚好有一个需求,然而我感觉我也没有办法用C++以外的语言去实现它.于是还是花了几天时间用C++完成编码,这是一个简单的任务执行引擎,它 ...
- 第4章 sed命令
1 sed命令基本用法 sed(stream editor)是流编辑器,可对文本文件和标准输入进行编辑: sed只是对缓冲区中原始文件的副本进行编辑,并不编辑原始的文件,如果需要保存改动内容,可以选择 ...
- 输出1——n的排列(深度优先搜索)
样例输入: 3 样例输出: 123132213231312321 #include <stdio.h> int n; void dfs(int step,int* a,int* book) ...
- javaScript 封装
在基于web 的b/s 架构的项目中, 丰富的界面都离不开 javascript, javascript 在 html 中变得越来越强大,但是我们在写 javascript 的时候都比较随意,随着页面 ...
- flask页面操作gpn接口
https://wizardforcel.gitbooks.io/flask-extension-docs/content http://cabeza.cn/blog/2016/02/28/datat ...