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 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.
思路: 并查集的题目,首先知道如何表示并查集的数据结构:
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(并查集)的更多相关文章
- 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 ...
- 04-树5. File Transfer--并查集
对于一个集合常见的操作有:判断一个元素是否属于一个集合:合并两个集合等等.而并查集是处理一些不相交集合(Disjoint Sets)的合并及查询问题的有利工具. 并查集是利用树结构实现的.一个集合用一 ...
- Uva 12361 File Retrieval 后缀数组+并查集
题意:有F个单词,1 <= F <=60 , 长度<=10^4, 每次可以输入一个字符串,所有包含该字串的单词会形成一个集合. 问最多能形成多少个不同的集合.集合不能为空. 分析:用 ...
- 05-树8 File Transfer
并查集 简单并查集:输入N,代表有编号为1.2.3……N电脑.下标从1开始.初始化为-1.合并后根为负数,负数代表根,其绝对值代表个数. We have a network of computers ...
- pat04-树5. File Transfer (25)
04-树5. File Transfer (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue We have ...
- 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 ...
- *HDU2473 并查集
Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- [并查集] POJ 1611 The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 35206 Accepted: 17097 De ...
- PAT 5-8 File Transfer (25分)
We have a network of computers and a list of bi-directional connections. Each of these connections a ...
随机推荐
- Shell笔记-01
打开文本编辑器,新建一个文件,扩展名为sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好,如果你用php写shell 脚本,扩展名就用php好了. 输入一些代码: #!/bin/bash ...
- oracle11g之Oracle体系结构(理论基础知识)
第二章 oracle的体系结构 一.oracle体系结构概述1.实例和数据库组成完整的Oracle数据库系统数据库:一系列物理文件的集合(数据文件,控制文件,联机日志,参数文件等)实例:一组oracl ...
- 404 Note Found 队-课堂实战-项目UML设计
目录 团队信息 分工选择 课上分工 课下分工 ToDolist alpha版本要做的事情 燃尽图 UML 用例图 状态图 活动图 类图 部署图 实例图 对象图 时序图 包图 通信图 贡献分评定 课上贡 ...
- flink Window的Timestamps/Watermarks和allowedLateness的区别
Watermartks是通过additional的时间戳来控制窗口激活的时间,allowedLateness来控制窗口的销毁时间. 注: 因为此特性包括官方文档在1.3-1.5版本均未做改变,所以 ...
- Laravel发送邮件配置-问题解决
MAIL_DRIVER=smtp MAIL_HOST=smtp..com MAIL_PORT= MAIL_USERNAME=phpfangzhan@@@@@.com MAIL_PASSWORD=*** ...
- spring boot++jpa+ mysql +maven
项目结构图: 一. 添加mysql 配置 1 在pom.xml中添加 maven依赖 <!--mysql--> <dependency> <groupId>mysq ...
- SSIS中出现数据流数据源假死状态的解决办法
相信开发过Sql Server SSIS的人都遇到过在数据流中数据源假死的问题,特别是Excel Source特别容易假死,当job执行到数据流中的Excel Source时,既不报错也不执行,也没有 ...
- layDay日期格式不合法报错解决
报错内容如下: Uncaught TypeError: Cannot read property 'appendChild' of undefined 相关报错内容的行代码如下 即使日期格式拼接正确也 ...
- c#的传输组件dotnetty
牛皮不多了,绩效吹起.... 最近一直看大家写的东西,了解的内容不少,我的牛皮也差不多吹完了.... 最后在说说最近测试的dotnetty.去年弄下来试了,不行,最近又弄下来了看看,可以了.哇哈哈哈哈 ...
- Vue中使用webpack别名的方法
在工作中,我们经常会写出这种代码: import MHeader from '../../components/m-header/m-header' @import "../../commo ...