File Transfer(并查集)
题目大意:将多个电脑通过网线连接起来,不断查询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≤104), 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.
思路: 并查集的题目,首先知道如何表示并查集的数据结构:
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(并查集)相关推荐
- 05-树8 File Transfer(并查集)
05-树8 File Transfer 分数 25 作者 陈越 单位 浙江大学 We have a network of computers and a list of bi-directional ...
- 信号放大器数据结构_[11/11]数据结构 二叉树应用(树型信号放大器,file transfer,遍历的非递归实现)...
树型分布网络信号放大器 森林和二叉树的相互转换 并查集 例题:File transfer #include <iostream> using namespace std; //typede ...
- c++自带的可持久化平衡树?rope大法好!(超详细解答 + 5道例题讲解,可直接替代可持久化的线段树、并查集、平衡树!)
整理的算法模板合集: ACM模板 目录 c++自带的可持久化平衡树?rope大法好! 1. 声明 2. 支持操作 char类型的rope int类型的rope 3. 具体的细节 4. "可持 ...
- 并查集 HDOJ 5441 Travel
题目传送门 题意:给一张无向图,问存在多少(a, b)表示a点到b点经过的边值小于等于x ((a,b) 和 (b, a)属于不同的方案) 分析:首先将边权值和查询x值升序排序,从前往后扫描边,累加从u ...
- 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 ...
- 【Python数据结构】——并查集的实现(查找、合并、集合、实例)
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/7/30 23:12 # @Author : @linlianqin # @S ...
- Ice_cream's world I HDU - 2120(并查集判环)
题意:问给出的望塔之间的建造了围墙,将土地分成了几份 思路:用并查集判环,若有围墙相接的瞭望塔,有相同的父根,则存在环 ice_cream's world is a rich country, it ...
- PAT 1114 Family Property 并查集
This time, you are supposed to help us collect the data for family-owned property. Given each person ...
- 【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 ...
最新文章
- luvit 被忽视的lua 高性能框架(仿nodejs)
- opencv运行时exe 无法写进去的解决方法
- 【Linux】一步一步学Linux——ip命令(183)
- Android入门之常用控件
- 全国计算机等级考试题库二级C操作题100套(第83套)
- 服务器宕机不再愁!Docker 内置功能帮您解决
- 山东大学网络教育计算机基础考试题,山东大学网络教育计算机网络基础期末考试复习...
- 【蓝桥杯单片机组】| DS1302(官方驱动)+ 矩阵按键 + 数码管(改,解决数码管跳动的问题)
- 计算机主板外部接口功能,笔记本电脑主板接口功能分享
- 关系模式设计优化(数据库学习重点,难点)
- win 10 把秘钥清掉之后查不到秘钥怎么办
- Zygote启动及其作用
- 抓取淘宝司法拍卖数据
- Java 如何从一个 List 中随机获得元素
- 解决J-LINk下载(sw接线方式)一次就不能下载了的问题
- 丘比特 脱单攻略之女神倒追
- 杂谈——什么是Google Fuchsia ?
- tiktok垂直账号运营经验分享
- 新品解读 | 融云超级群何以「超级」?
- 【Tree-easy】617. Merge Two Binary Trees 合并两个二叉树
热门文章
- Hadoop的学习前奏(二)——Hadoop集群的配置
- Gentoo 安装日记 15 (配置内核 :固件驱动..文件系统以及其他)
- [转]Redhat EL5.4用CentOS源进行更新
- 一分钟明白各种SQL语句加的什么锁——《深究Mysql锁》
- B. super_log(2019ICPC区域网络赛南京站)
- html伪类选择器代码,CSS3中结构性伪类选择器—:first-of-type实现名言标签(代码实例 )...
- python 栈和队列_Python实现栈和队列的简单操作方法示例
- 把阿里巴巴的核心系统搬到云上,架构上的挑战与演进是什么?
- linux永久改变字符集,Linux 下mysql永久更改字符集
- java el ognl_el表达式跟ognl表达式的区别