pat04-树5. File Transfer (25)
04-树5. 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.
并查集
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
int f[];
int findfa(int a){
if(f[a]!=a){
f[a]=findfa(f[a]);
}
return f[a];
}
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n;
//memset(l,-1,sizeof(l));
int i,a,b,fa,fb;
char c;
scanf("%d",&n);
for(i=;i<=n;i++){
f[i]=i;
}
for(i=;;i++){
cin>>c;
if(c=='S'){
break;
}
cin>>a>>b;
fa=findfa(a);
fb=findfa(b);
if(c=='I'){
if(fa!=fb){
if(fa>fb){
f[fb]=fa;
}
else{
f[fa]=fb;
}
}
}
else{
if(fa==fb){
printf("yes\n");
}
else{
printf("no\n");
}
}
}
int num=;
for(i=;i<=n;i++){
if(f[i]==i){
num++;
}
}
if(num==){
printf("The network is connected.\n");
}
else{
printf("There are %d components.\n",num);
}
return ;
}
pat04-树5. File Transfer (25)的更多相关文章
- 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 ...
- PAT 5-8 File Transfer (25分)
We have a network of computers and a list of bi-directional connections. Each of these connections a ...
- 05-树8 File Transfer (25 分)
We have a network of computers and a list of bi-directional connections. Each of these connections a ...
- 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 ...
- 05-树8 File Transfer (25 分)
We have a network of computers and a list of bi-directional connections. Each of these connections a ...
- File Transfer(并查集)
题目大意:将多个电脑通过网线连接起来,不断查询2台电脑之间是否连通. 问题来源:中国大学mooc 05-树8 File Transfer (25 分) We have a network of com ...
- File Transfer
本博客的代码的思想和图片参考:好大学慕课浙江大学陈越老师.何钦铭老师的<数据结构> 代码的测试工具PTA File Transfer 1 Question 2 Explain First, ...
- 让 File Transfer Manager 在新版本WIndows上能用
最近研究.NET NATIVE,听说发布了第二个预览版,增加了X86支持,所以下,发现连接到的页面是:https://connect.microsoft.com/VisualStudio/Downlo ...
- PAT 05-树7 File Transfer
这次的题让我对选择不同数据结构所产生的结果惊呆了,一开始用的是结构来存储集合,课件上有现成的,而且我也是实在不太会,150ms的时间限制过不去,不得已,看到这题刚好可以用数组,结果7ms最多,有意思! ...
随机推荐
- oracle安装和使用问题解决方案
1.Enter the full pathname for java.exe 要输入的是32位系统的jdk中的java.exe路径,比如 C:\Program Files\Java\jdk1.6.0 ...
- MVC小记备忘
1,页面用<dl><dt><dd>和Bootstrap的"dl-horizontal"类布局页面,使每一个属性名和属性值占一行 <dl c ...
- Django之高级视图与URL
Urls.py中有我们所有的路由映射关系.但是随着网站功能的增多,需要配置的URL也变得多了起来.并且维护这些导入也变得很麻烦.因此介绍一些优化和简洁的方法: 一 使用多个视图前缀: 之前所有的路由关 ...
- ABAP F4使用总结!!
PARAMETERS: p_werks LIKE t001l-werks , p_lgort LIKE t001l-lgort . SELECTION-SCREEN SKIP. ...
- 牛客寒假算法基础集训营4 G Applese 的毒气炸弹
链接:https://ac.nowcoder.com/acm/contest/330/G来源:牛客网 众所周知,Applese 是个很强的选手,它的化学一定很好. 今天他又AK了一套题觉得很无聊,于是 ...
- C语言预处理命令之文件包含
文件包含预处理命令的一般形式是: #include<文件名> 或者 #include“文件名” #include命令告诉预处理器用指定文件的内容替换这条命令,两种不同的命令格式决定了预处理 ...
- Mybatis学习笔记(三) —— DAO开发方法
一.SqlSession的使用范围 SqlSession中封装了对数据库的操作,如:查询.插入.更新.删除等. SqlSession通过SqlSessionFactory创建. SqlSessionF ...
- Django2.0以上版本的URL的正则匹配问题
正则表达式形式的URL匹配问题: 1.导入re_path from django.urls import path,re_path 2.进行正则匹配 re_path('detail-(\d+).htm ...
- js 点击页面出现烟花 心形
css :1 body{width:1000px; height:500px;} input{ margin:100px; width:250px; height: 50px } 3 .Firewor ...
- P4245 【模板】任意模数NTT
Luogu4245 只要做三次的NTT,快的飞起 普通NTT,做9次 #include<cstdio> #include<cstring> #include<iostre ...