05-树8 File Transfer(并查集)
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.
1)并查集:(按秩合并,路径压缩);
/**1)并查集:(按秩合并,路径压缩);
*/#include <iostream>
#include <string>using namespace std;const int maxn=1e4+10;
int fath[maxn]={0};int Find(int x);
void Union(int x,int y);int main()
{int n;cin >> n;string op;int a,b;while(cin >> op,op!="S"){if(op=="C"){cin >> a >> b;a=Find(a),b=Find(b);if(a==b)cout << "yes\n";elsecout << "no\n";}else{cin >> a >>b;Union(a,b);}}int flag=0;for(int i=1;i<=n;++i)if(fath[i]==0)++flag;if(flag==1)cout << "The network is connected.\n";elseprintf("There are %d components.",flag);return 0;
}int Find(int x)
{if(fath[x]==0)return x;elsereturn fath[x]=Find(fath[x]);
}void Union(int x,int y)
{x=Find(x);y=Find(y);if(x!=y)fath[x]=y;
}
2)我下次再遇到操作符与运算符在一行的时候,我一定分开写,不用string一起读入,
后面再取运算符,两次都是犯这种错误,样例给的运算符是个位数,就想当然的把所有
数字都想成了单数,我真是……
直接用int 输入发便许多。
/**2)我下次再遇到操作符与运算符在一行的时候,我一定分开写,不用string一起读入,后面再取运算符,两次都是犯这种错误,样例给的运算符是个位数,就想当然的把所有数字都想成了单数,我真是……直接用int 输入发便许多。
*//**
#include <iostream>
#include <cstring>
#include <string>using namespace std;const int maxn=1e4+10;
int fath[maxn]={0};int Find(int x);
void Union(int x,int y);int main()
{memset(fath,-1,sizeof(fath));int n;cin >> n;getchar();string str;while(getline(cin,str),str!="S"){if(str[0]=='C'){int a=0,b=0;int index=2; //我下次再遇到操作符与运算符在一行的时候,我一定分开写,不用string一起读入,后面再取运算符,两次都是犯这种错误,样例给的运算符是个位数,就想当然的把所有数字都想成了单数,我真是……for(index=2;index<str.size()&&str[index]!=' ';++index)a=a*10+str[index]-'0';for(++index;index<str.size();++index)b=b*10+str[index]-'0';a=Find(a),b=Find(b);if(a==b)cout << "yes\n";elsecout << "no\n";}else{int a=0,b=0;int index=2;for(index=2;index<str.size()&&str[index]!=' ';++index)a=a*10+str[index]-'0';for(++index;index<str.size();++index)b=b*10+str[index]-'0';Union(a,b);}}int flag=0;for(int i=1;i<=n;++i)if(fath[i]<0)++flag;if(flag==1)cout << "The network is connected.\n";elseprintf("There are %d components.",flag);return 0;
}int Find(int x)
{if(fath[x]<0)return x;elsereturn fath[x]=Find(fath[x]);
}void Union(int x,int y)
{x=Find(x);y=Find(y);if(x!=y){if(fath[x]<fath[y]){fath[x]+=fath[y];fath[y]=x;}else{fath[y]+=fath[x];fath[x]=y;}}
}
*/
05-树8 File Transfer(并查集)相关推荐
- File Transfer(并查集)
题目大意:将多个电脑通过网线连接起来,不断查询2台电脑之间是否连通. 问题来源:中国大学mooc 05-树8 File Transfer (25 分) We have a network of com ...
- 数据结构之树的应用:并查集
树的应用:并查集 并查集的概念: 三种基本操作: 例: 代码实现: 并查集的概念: 将所有的数据元素放在一个集合中,将集合分成若干个互不相交的子集,每一个子集对应一颗树,所有的自己组成森林. 三种基本 ...
- 线段树分治 ---- F. Extending Set of Points(线段树分治 + 可撤销并查集)
题目链接 题目大意: 你有个点集合SSS,每次往集合里面加点或者删点(如果要加的点出现过),如果(x1,y1),(x2,y1),(x1,y2),(x2,y2)(x1,y1),(x2,y1),(x1,y ...
- BZOJ 2143 飞飞侠(线段树优化建边 / 并查集优化最短路)【BZOJ修复工程】
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 题目链接 https://hydro.ac/d/bzoj/p/2143 是 hydro 的 BZOJ ...
- PAT甲级1021 Deepest Root :[C++题解]树的最大深度、并查集、dfs求树的深度
文章目录 题目分析 题目链接 题目分析 分析: 考察知识点:并查集.dfs.树的深度 给定n个结点,n-1条边,只要能保证只有1个连通分量,就是一棵树.否则的话就不是树,它是不连通的. 用并查集来看是 ...
- 51nod1307(暴力树剖/二分dfs/并查集)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 中文题诶~ 思路: 解法1:暴力树剖 用一个数 ...
- 【CF813F】Bipartite Checking(线段树分治+可删除并查集)
文章目录 title solution code title You are given an undirected graph consisting of n vertices. Initially ...
- BZOJ4399 魔法少女LJJ【线段树合并】【并查集】
Description 在森林中见过会动的树,在沙漠中见过会动的仙人掌过后,魔法少女LJJ已经觉得自己见过世界上的所有稀奇古怪的事情了 LJJ感叹道"这里真是个迷人的绿色世界,空气清新.淡雅 ...
- 可持久化线段树【主席树】可持久化并查集【主席树+并查集】
笼统的主席树原理 众所周知, 主席树是可以持久化的, 换言之你能知道你所维护信息的所有历史状态. 主席树是这样做的: 1. 首先建一颗朴素的线段树,代表初始状态 (下图黑色) , 也就是第0次操作后的 ...
- tire树的存储和并查集
tire树 tire树又称字典树,是一种能够高效存储和查找字符串集合的数据结构. 图形如下图所示 每个节点表示一个字符串中的字符,从根节点到灰色节点的一条路径表示一个字符串(灰色节点表示是某个单词的结 ...
最新文章
- bzoj3442 学习小组
- php8支持哪些新特征,PHP8 新特性总结(一)
- AgileGAN130毫秒生成动漫肖像!LeCun点赞:超越梵高
- JAVA8常量池监控_深入探索Java常量池
- Python学习之路1 - 基础入门
- Android源码设计模式分析项目
- open source ecg_苹果watchOS 7.1 正式版发布:ECG功能上线多国
- matlab tf离散,求matlab离散化程序对于一个二阶传函,求其在MATLAB中的离散化程序.抱歉,没办法写清楚传函表达式.我试试:Gp(...
- 应变界的翘楚:硅基谐振式传感器灵敏度非常高
- 获得显示器设置的分辨率
- 测屏幕坏点 android,手机屏幕坏点检测方法有哪些【详细介绍】
- 教你炒股票13:不带套的操作不是好操作!
- FFmpeg —— 为视频添加文字水印(完美解决中文乱码,无需编译)
- Matlab中计算图像的灰度值
- iOS 两张图片合并成一张。
- 如何才是一名优秀的好员工
- Codefoeces 581B Luxurious Houses
- python毕业论文开题报告_本科毕业论文开题报告怎么写-如何写毕业设计的开题报告?...
- 基于百度地图sdk的地图app开发(七)——导航和模拟导航
- 用VBA做一个儿童识字,数字考察游戏(持续更新中)
热门文章
- 苹果cms视频文字采集插件
- 关于XGBoost训练数据clf.fit(x_train, y_train)时报错
- ipfs搭建mysql_IPFS环境搭建节点安装
- Bamboo项目自动化构建
- 微晓注册表优化大师 v2.02 bt
- 选择区域的检验tajimD
- 纪中集训2020.02.05【NOIP提高组】模拟B 组总结反思——【佛山市选2010】组合数计算,生成字符串 PPMM...
- ps2019布尔运算快捷键_ps布尔运算怎么用【搞定要领】
- leetcode 刷题指南 刷题顺序
- Linux 性能优化实战(倪朋飞)---平均负载