题目大意:将多个电脑通过网线连接起来,不断查询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=0;i<MaxSize&&S[i].data!=x;i++);     //查找的时间复杂度nif(i>MaxSize)return -1;for(;S[i].parent>=0;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 10000int S[Maxitem];
//采用路径压缩,尾递归寻找他的根结点
int find(int x){if(S[x]<0)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 10000int S[Maxitem];
//采用路径压缩,尾递归寻找他的根结点
int find(int x){if(S[x]<0)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=0;i<n;i++){S[i]=-1;}
}
void Input_connection(){int u,v,root1,root2;scanf("%d %d",&u,&v);getchar();root1=find(u-1);root2=find(v-1);Union(root1,root2);
}
void Check_connection(){int u,v,root1,root2;scanf("%d %d",&u,&v);getchar();root1=find(u-1);root2=find(v-1);if(root1==root2)printf("yes\n");else printf("no\n");
}
void Check_networks(int n){int count=0;for(int i=0;i<n;i++)if(S[i]<0)count++;if(count==1)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 0;
}

 

转载于:https://www.cnblogs.com/patatoforsyj/p/9828473.html

File Transfer(并查集)相关推荐

  1. 05-树8 File Transfer(并查集)

    05-树8 File Transfer 分数 25 作者 陈越 单位 浙江大学 We have a network of computers and a list of bi-directional ...

  2. 信号放大器数据结构_[11/11]数据结构 二叉树应用(树型信号放大器,file transfer,遍历的非递归实现)...

    树型分布网络信号放大器 森林和二叉树的相互转换 并查集 例题:File transfer #include <iostream> using namespace std; //typede ...

  3. c++自带的可持久化平衡树?rope大法好!(超详细解答 + 5道例题讲解,可直接替代可持久化的线段树、并查集、平衡树!)

    整理的算法模板合集: ACM模板 目录 c++自带的可持久化平衡树?rope大法好! 1. 声明 2. 支持操作 char类型的rope int类型的rope 3. 具体的细节 4. "可持 ...

  4. 并查集 HDOJ 5441 Travel

    题目传送门 题意:给一张无向图,问存在多少(a, b)表示a点到b点经过的边值小于等于x ((a,b) 和 (b, a)属于不同的方案) 分析:首先将边权值和查询x值升序排序,从前往后扫描边,累加从u ...

  5. Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]

    传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  6. 【Python数据结构】——并查集的实现(查找、合并、集合、实例)

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/7/30 23:12 # @Author : @linlianqin # @S ...

  7. Ice_cream's world I HDU - 2120(并查集判环)

    题意:问给出的望塔之间的建造了围墙,将土地分成了几份 思路:用并查集判环,若有围墙相接的瞭望塔,有相同的父根,则存在环 ice_cream's world is a rich country, it ...

  8. PAT 1114 Family Property 并查集

    This time, you are supposed to help us collect the data for family-owned property. Given each person ...

  9. 【PAT - 甲级1034】Head of a Gang (30分)(并查集)

    题干: One way that the police finds the head of a gang is to check people's phone calls. If there is a ...

最新文章

  1. luvit 被忽视的lua 高性能框架(仿nodejs)
  2. opencv运行时exe 无法写进去的解决方法
  3. 【Linux】一步一步学Linux——ip命令(183)
  4. Android入门之常用控件
  5. 全国计算机等级考试题库二级C操作题100套(第83套)
  6. 服务器宕机不再愁!Docker 内置功能帮您解决
  7. 山东大学网络教育计算机基础考试题,山东大学网络教育计算机网络基础期末考试复习...
  8. 【蓝桥杯单片机组】| DS1302(官方驱动)+ 矩阵按键 + 数码管(改,解决数码管跳动的问题)
  9. 计算机主板外部接口功能,笔记本电脑主板接口功能分享
  10. 关系模式设计优化(数据库学习重点,难点)
  11. win 10 把秘钥清掉之后查不到秘钥怎么办
  12. Zygote启动及其作用
  13. 抓取淘宝司法拍卖数据
  14. Java 如何从一个 List 中随机获得元素
  15. 解决J-LINk下载(sw接线方式)一次就不能下载了的问题
  16. 丘比特 脱单攻略之女神倒追
  17. 杂谈——什么是Google Fuchsia ?
  18. tiktok垂直账号运营经验分享
  19. 新品解读 | 融云超级群何以「超级」?
  20. 【Tree-easy】617. Merge Two Binary Trees 合并两个二叉树

热门文章

  1. Hadoop的学习前奏(二)——Hadoop集群的配置
  2. Gentoo 安装日记 15 (配置内核 :固件驱动..文件系统以及其他)
  3. [转]Redhat EL5.4用CentOS源进行更新
  4. 一分钟明白各种SQL语句加的什么锁——《深究Mysql锁》
  5. B. super_log(2019ICPC区域网络赛南京站)
  6. html伪类选择器代码,CSS3中结构性伪类选择器—:first-of-type实现名言标签(代码实例 )...
  7. python 栈和队列_Python实现栈和队列的简单操作方法示例
  8. 把阿里巴巴的核心系统搬到云上,架构上的挑战与演进是什么?
  9. linux永久改变字符集,Linux 下mysql永久更改字符集
  10. java el ognl_el表达式跟ognl表达式的区别