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最多,有意思! ...
随机推荐
- 从零开始搭建.NET Core 2.0 API(学习笔记一)
从零开始搭建.NET Core 2.0 API(学习笔记一) 一. VS 2017 新建一个项目 选择ASP.NET Core Web应用程序,再选择Web API,选择ASP.NET Core 2. ...
- 动态合并GridView数据行DataRow的列
前段时间,Insus.NET一直在演示GridView控件Header头行或列:<动态变更GridView控件列名>和<动态合并或定制GridView控件Header头某些列> ...
- 教学ppt设计与制作
分三部分陈述,分别是元素呈现/版面设计/导航设计/. 1 元素呈现 文本 (1)字符表现策略 少用宋体 多用黑体,微软雅黑等 少用艺术字 (2)段落表现策略 条目化 一行一条 用符号简化文字描述 按有 ...
- 写RestApi需要注意些什么?
PS1="\n[\e[32;1m]([\e[37;1m]\u[\e[32;1m])-([\e[37;1m]jobs:\j[\e[32;1m])-([\e[37;1m]\w[\e[32;1m] ...
- 3分钟实现iOS语言本地化/国际化(图文详解)
前言 语言本地化,又叫做语言国际化. 是指根据用户操作系统的语言设置,自动将应用程序的语言设置为和用户操作系统语言一致的语言. 往往一些应用程序需要提供给多个国家的人群使用,或者一个国家有多种语言,这 ...
- Python之图片缩放功能实现
这几天由于有项目在做,自己的学习部分然后没有很充足的时间,但是这些零碎的时间也是很宝贵的,所以还是继续学我的python,我很喜欢这个语言,因为简洁,开发环境简单,更多的事,功能灰常的强大,所以好多有 ...
- leftjoin及多个leftjoin执行顺序
给个通俗的解释吧.例表aaid adate1 a12 a23 a3表bbid bdate1 b12 b24 b4两个表a,b相连接,要取出id相同的字段select * from a inner jo ...
- 老男孩Day6作业:计算器
作业需求: 1.实现加减乘除及拓号优先级解析 2.用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) ...
- Codeforces Round #521 (Div. 3) C. Good Array
C. Good Array time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Qt 学习之路 2(3):Hello, world!
豆子 2012年8月22日 Qt 学习之路 2 107条评论 想要学习 Qt 开发,首先要搭建 Qt 开发环境.好在现在搭建 Qt 开发环境还是比较简单的.我们可以到 Qt 官方网站找到最新版 ...