题目大意:将多个电脑通过网线连接起来,不断查询2台电脑之间是否连通。

问题来源:中国大学mooc

05-树8 File Transfer (25 分)

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

思路: 并查集的题目,首先知道如何表示并查集的数据结构:

typedef struct{
  int data;
  int parent;
}SetType[MaxSize];
本题可以进行优化:只用一个数组  int SetType[MaxSize]表示即可,数组下标表示计算机的序号,SetType[index]表示为他的父节点,就是连通节点的序号。
并查集的Find  和union函数如下:
int Find(SetType S[],int x){
int i;
for(i=;i<MaxSize&&S[i].data!=x;i++); //查找的时间复杂度n
if(i>MaxSize)return -;
for(;S[i].parent>=;i=S[i].parent);
return i; //找到X所属集合,返回树根结点在数组S中的下标
} void Union(SetType S[],int x1,int x2){
int root1,root2;
root1=find(S,x1);
root2=find(S,x2);
if(root1!=root2)S[root2]=root1; }

优化后的Find和路径压缩函数如下:

#define Maxitem 10000

int S[Maxitem];
//采用路径压缩,尾递归寻找他的根结点
int find(int x){
if(S[x]<)return x;
////先找到根; 把根变成 X 的父结点; 再返回根。
else return S[x]=find(S[x]);
}
//按秩归并,将根结点数量少的树连接到根结点多的树,这里用S[root]表示根结点
//S[root]为负数,绝对值为节点数
void Union(int root1,int root2){
if(S[root1]>S[root2]){
S[root2]+=S[root1];
S[root1]=root2;
}else {
S[root1]+=S[root2];
S[root2]=root1;
}
}

程序框架:

最终代码如下:

#include<cstdio>
#define Maxitem 10000 int S[Maxitem];
//采用路径压缩,尾递归寻找他的根结点
int find(int x){
if(S[x]<)return x;
////先找到根; 把根变成 X 的父结点; 再返回根。
else return S[x]=find(S[x]);
}
//按秩归并,将根结点数量少的树连接到根结点多的树,这里用S[root]表示根结点
//S[root]为负数,绝对值为节点数
void Union(int root1,int root2){
if(S[root1]>S[root2]){
S[root2]+=S[root1];
S[root1]=root2;
}else {
S[root1]+=S[root2];
S[root2]=root1;
}
}
void initialzation(int n){
for(int i=;i<n;i++){
S[i]=-;
}
}
void Input_connection(){
int u,v,root1,root2;
scanf("%d %d",&u,&v);
getchar();
root1=find(u-);
root2=find(v-);
Union(root1,root2);
}
void Check_connection(){
int u,v,root1,root2;
scanf("%d %d",&u,&v);
getchar();
root1=find(u-);
root2=find(v-);
if(root1==root2)printf("yes\n");
else printf("no\n");
}
void Check_networks(int n){
int count=;
for(int i=;i<n;i++)
if(S[i]<)count++;
if(count==)printf("The network is connected.\n");
else printf("There are %d components.\n",count);
}
int main(){
char in;
int n;
scanf("%d",&n);
initialzation(n);
do{
scanf("%c",&in);
switch(in){
case'I':Input_connection();break;
case'C':Check_connection();break;
case'S':Check_networks(n);break;
}
}while(in!='S');
return ;
}
 

File Transfer(并查集)的更多相关文章

  1. 05-树8 File Transfer(25 point(s)) 【并查集】

    05-树8 File Transfer(25 point(s)) We have a network of computers and a list of bi-directional connect ...

  2. 04-树5. File Transfer--并查集

    对于一个集合常见的操作有:判断一个元素是否属于一个集合:合并两个集合等等.而并查集是处理一些不相交集合(Disjoint Sets)的合并及查询问题的有利工具. 并查集是利用树结构实现的.一个集合用一 ...

  3. Uva 12361 File Retrieval 后缀数组+并查集

    题意:有F个单词,1 <= F <=60 , 长度<=10^4, 每次可以输入一个字符串,所有包含该字串的单词会形成一个集合. 问最多能形成多少个不同的集合.集合不能为空. 分析:用 ...

  4. 05-树8 File Transfer

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

  5. pat04-树5. File Transfer (25)

    04-树5. File Transfer (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue We have ...

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

  7. *HDU2473 并查集

    Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  8. [并查集] POJ 1611 The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 35206   Accepted: 17097 De ...

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

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

随机推荐

  1. C#中参数传递

    当调用带有参数的方法,需要向方法传递参数,有三种向方法传递参数的方式. 1.值参数:这种方式复制参数的实际值给形式参数,形参和实参使用的是内存中两个不相同的值,形参发生改变不会影响实参的值,从而保证了 ...

  2. 前端基础-HTML的的标签详解

    阅读目录 一.head内常用标签 二. HTML语义化 三. 字符实体 四. h系列标签 五. p标签 六. img标签 七. a标签 八. 列表标签 九. table标签 十. form标签 一. ...

  3. git私服

    目录 安装 git 在服务器上部署 Git 1.在服务器上创建一个新用户 2.创建一个git仓库 3.在服务器端打开RSA认证(重要) 4.在客户端创建SSH key 5.把步骤4生成的公钥导入服务器 ...

  4. sqlplus连接半天才连上

    问题现象: 某oracle数据库服务器发现使用ssh,crt连接半天1-2分钟后才返回输入密码的提示,应用人员发现使用 sys_GUID()函数获取唯一值的时候,第一次调用需要等待很长时间,但是同一s ...

  5. ODI使用流程

    ---恢复内容开始--- ODI流程 Topology 1.建立 源 物理结构体系 2.建立 目的 物理结构体系 步骤同上 3.建立 源 逻辑架构 4.建立 目的 逻辑架构 步骤同上 Designer ...

  6. linux下环境变量PS1-命令提示符

    1.字体颜色 1.1颜色及对应数字 颜色表 前景 背景 颜色 30     40   黑色 echo -e "\e[30mforegroud\e[m\e[40mbackground\e[m& ...

  7. 高性能MySQL--创建高性能的索引

    关于MySQL的优化,相信很多人都听过这一条:避免使用select *来查找字段,而是要在select后面写上具体的字段. 那么这么做的原因相信大家都应该知道:减少数据量的传输. 但我要讲的是另外一个 ...

  8. Qt的checkbox风格修改

    环境: HelperA64开发板 Linux3.10内核 时间:2019.01.12 目标:修改Qt中checkbox图片太小的BUG 问题: 在从Qt4移植为Qt5时遇到很多问题,这次是移植到开发板 ...

  9. IOS 可以连接 蓝牙BLE设备,但是无法发现服务(原创)

    注:转载请标明文章来源,感谢支持作者劳动! 一.问题描述 用iphone手机上的nRF connect软件调试蓝牙通信. 1.nRF52蓝牙demo电路板,烧录一个SDK的程序,iphone手机可以成 ...

  10. web安全入门笔记

    0x01 前言 这正邪两字,原本难分. 正派弟子若是心术不正,便是邪徒. 邪派中人只要一心向善,便是正人君子. 0x01 信息安全的定义 信息安全,意为保护信息及信息系统免受未经授权的进入.使用.披露 ...