并查集

简单并查集:输入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. dubbo服务框架学习

    ====================================================================================== 1.提供注册服务.消费者可 ...

  2. maven与git

    ================================================== maven的作用 ======================================== ...

  3. 修改 sql server 2008R2的端口,配置防火墙允许远程访问SQL Server 2008 R2

    1.先修改 sql server 2008R2的端口号吧,1433经常成为别人入侵的端口,在sql server 配置管理器 -->sql server 网络配置-->MSSQLSERVE ...

  4. (旧)子数涵数·PS——水杯抠图

    一.首先老规矩,下载所需要的素材. 二.打开Photoshop,并打开已下载好的素材. 三.使用"钢笔工具",快捷键为P,采用"路径"模式,将水杯抠出(例图左上 ...

  5. DoTween使用

    官网:http://dotween.demigiant.com/ 1.step 这里使用lamda表达式,通过dotween的to方法将其移动到 Vector3(348, 196, 0)的值返回到Ve ...

  6. [原创]大连sap vt 实习生面试经历

    从决定参加这次面试开始,求职生涯就算是开始了,虽然失败了,但也是亲身体会到了面试的感觉,这次经历也作为第一篇博客. 大连sap的职能是sap的全球技术支持中心和解决方案提供中心.VT项目提供的岗位是技 ...

  7. C#如何在事件中获得GridView里面TextBox的值

    GridView设置如下: <asp:GridView ID="GridViewlb" runat="server" AutoGenerateColumn ...

  8. php函数将对象转换为数组以及json_decode的使用技巧

    在写php代码的时候,发现调用接口返回的结果类型是对象,不是自己想要的数组,于是乎,写了一个将对象转化数组的函数: /** * 将对象转化为数组 */ private function objectT ...

  9. 学习总结 java Iterator迭代器练习

    package com.hanqi.jh; import java.util.*; public class Text3 { public static void main(String[] args ...

  10. Android 操作系统的内存回收机制

    参考 http://www.ibm.com/developerworks/cn/opensource/os-cn-android-mmry-rcycl/index.html