并查集

简单并查集:输入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 c1and 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 kis 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的更多相关文章

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

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

  2. File Transfer

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

  3. File Transfer(并查集)

    题目大意:将多个电脑通过网线连接起来,不断查询2台电脑之间是否连通. 问题来源:中国大学mooc 05-树8 File Transfer (25 分) We have a network of com ...

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

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

  5. PAT 05-树7 File Transfer

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

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

  7. Trivial File Transfer Protocol (TFTP)

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

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

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

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

随机推荐

  1. 在RAC中,当私有网线拔了后,会怎么样?

    原文链接http://blog.mchz.com.cn/?p=4305 实际环境: OS:oel5.5_x64 ORACLE:10205 3节点rac 架设于vmware esxi虚拟机上 所需测试项 ...

  2. python3 内置函数 filter()

    filter(function or None, iterable) --> filter object Return an iterator yielding those items of i ...

  3. github添加ssh认证

    总概:在使用git的时候,和目标仓库建立关系有两种方式https,ssh.一般用的是https认证(这样简单方便),但有个缺点,pull,push等操作需要频繁输入用户验证.虽然可以把用户验证账号密码 ...

  4. centos 6.5 samba简单配置

    1.安装samba yum -y install samba  (我的显示已经安装啦!) 2.编辑samba的配置文件 vi /etc/samba/smb.conf 用 testparm查看我配置后的 ...

  5. MSP430推荐网站

    http://www.amobbs.com/thread-5092914-1-1.html http://www.amobbs.com/thread-4701106-1-1.html

  6. [Oracle] 中的Temporary tablespace的作用

    临时表空间主要用途是在数据库进行排序运算[如创建索引.order by及group by.distinct.union/intersect/minus/.sort-merge及join.analyze ...

  7. 二模08day1解题报告

    T1.高中运动会(match) N个数的最大公约数. gcd不解释. T2.智力游戏 火柴棒等式形如a+b=c,现在给出啊a,b,c求使等式成立的最小的移动次数. 火柴棒表示数字不用解释了吧,在此提醒 ...

  8. MyBatis学习系列二——增删改查

    目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring 数据库的经典操作:增删改查. 在这一章我们主要说明一下简单的查询和增删改, ...

  9. if [-f build/core/envsetup.mk -a -f Makefile ]; then

    这个语法是什么意思?

  10. .net 调用SAP RFC函数获取数据的两种方式

    方式1:使用客户端自带的组件 安装客户端以后,添加引用:SAPFunctionsOCX(.net 的Com列表里一般找不到,需要引用DLL[一般位于以下路径:Program Files\SAP\Fro ...