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 ...
随机推荐
- JavaScript对象的创建之使用json格式定义
json: javascript simple object notation. json就是js的对象,但是它省去了xml中的标签,而是通过{}来完成对象的说明. 定义对象 var person = ...
- Eclipse Gtk+
From: http://hi.baidu.com/spmno/item/9425018707f295dfd1f8cdbe 1. Project->Properties,然后选 C/C++ Bu ...
- 将本地仓库托管到GitHub
在初始化仓库之前,要确认ssh key是否正确 ssh -T git@github.com 正确的结果如下 Hi username! You've successfully authenticated ...
- [译]使用AES 256以达到SSL/TLS安全最大化
原文链接:https://luxsci.com/blog/256-bit-aes-encryption-for-ssl-and-tls-maximal-security.html 原文发表时间:201 ...
- UIBlurEffect实现模糊效果
//使用图片初始化背景 Pattern 图案,模式 self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageN ...
- 简单jQuery实现选项框中列表项的选择
这段代码非常的简单,仅仅作为自己的一个小小的记录! ok,先上一个简单的图例,效果如下(注意:这只是一个简单的例子,不过可以根据这个简单的例子,变化出更为复杂的效果)! 代码也非常的简单,如下所示(注 ...
- 条码解析的一片js
function HIBC_CheckCode(code) { var nonCheckCode = code.substr(0, code.length - 1); var arr ...
- C++中的位运算总结
1)位运算 位运算是指对转换成二进制的数字进行每一位上的0.1的运算,运算涉及到五种运算:与(&),或(|),异或(^),左移(<<),右移(>>). 如下表所示: ...
- C# 加密算法
public static class Common { #region MD5加密 /// <summary> /// M ...
- MS SqlSever一千万条以上记录分页数据库优化经验总结【索引优化 + 代码优化】[转]
对普通开发人员来说经常能接触到上千万条数据优化的机会也不是很多,这里还是要感谢公司提供了这样的一个环境,而且公司让我来做优化工作.当数据库中的记录不超过10万条时,很难分辨出开发人员的水平有多高,当数 ...