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), 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>
const int maxn = ; void Initialization( int n);
void Input_connection();
int FindFather(int x);
void Check_connection();
void Check_network(int n);
int arr[maxn]; int main()
{
int n;
char in; scanf("%d",&n); Initialization(n); do
{
getchar();
scanf("%c",&in);
switch(in)
{ case 'I':
{
Input_connection();
break;
}
case 'C':
{
Check_connection();
break;
}
case 'S':
{
Check_network(n);
break;
}
}
}while (in != 'S');
return ; } void Initialization( int n)
{
for (int i = ; i <= n; i++)
{
arr[i] = i;
}
} void Input_connection()
{
int u,v;
scanf("%d %d",&u,&v);
int faA = FindFather(u);
int faB = FindFather(v);
if (faA != faB)
{
arr[faA] = faB;
}
} int FindFather(int x)
{
if (arr[x] == x)
{
return x;
}
else
{
return arr[x] = FindFather(arr[x]);
}
} void Check_connection()
{
int u,v;
scanf("%d %d",&u,&v);
int faA = FindFather(u);
int faB = FindFather(v);
if (faA != faB)
{
printf("no\n");
}
else
{
printf("yes\n");
}
} void Check_network(int n)
{
int cnt = ;
for (int i = ; i <= n; i++)
{
if (arr[i] == i)
{
cnt++;
}
} if ( == cnt)
{
printf("The network is connected.");
}
else
{
printf("There are %d components.",cnt);
}
}
05-树8 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 ...
- L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...
- pat04-树5. File Transfer (25)
04-树5. File Transfer (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue We have ...
- L2-006 树的遍历 (25 分)
链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 题目: 给定一棵二叉树的后序遍历和中序 ...
- 7-3 树的同构(25 分) JAVA
给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的. 例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后,就得到另外一棵树 ...
- 05-树8 File Transfer (25 分)
We have a network of computers and a list of bi-directional connections. Each of these connections a ...
- PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)
1021 Deepest Root (25 分) A graph which is connected and acyclic can be considered a tree. The heig ...
- 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 ...
随机推荐
- 关于VS2019使用Git时遇到的Bug
最近在运行vs2017时老是卡死,神烦!迫于无奈我从微软官网下载了vs2019,安装后打开项目,发现从git上拉取代码时报错如下: Please make sure you have the corr ...
- 树莓派安装使用docker
2019/11/11, 树莓派4B, Raspbian Buster,Docker 19.03.4 摘要:树莓派Raspbian Buster中安装Docker,Dockerfile更改软件源 安装d ...
- 面向对象——组合、封装、访问限制机制、property内置装饰器
面向对象--组合.封装.访问限制机制.property 组合 什么是组合? 组合指的是一个对象中,包含另一个或多个对象 为什么要组合? 减少代码的冗余 怎么用组合? # 综合实现 # 父类 class ...
- spark任务在executor端的运行过程分析
CoarseGrainedExecutorBackend 上一篇,我们主要分析了一次作业的提交过程,严格说是在driver端的过程,作业提交之后经过DAGScheduler根据shuffle依赖关系划 ...
- jemeter学习-badboy录制与代理服务器录制
一 基本元素的介绍 1. 添加测试计划 2.添加线程组 线程数---并发数,模拟多少个用户并发 Ramp-up periods ----我们要在多少秒之内进行多少用户的并发 循环次数---可以选择一次 ...
- LP线性规划初识
认识LP 线性规划(Linear Programming) 特指目标函数和约束条件皆为线性的最优化问题. 目标函数: 多个变量形成的函数 约束条件: 由多个等式/不等式形成的约束条件 线性规划: 在线 ...
- js 压缩图片 上传
感谢,参考了以下作者的绝大部分内容 https://blog.csdn.net/tangxiujiang/article/details/78755292 https://blog.csdn.net/ ...
- Docker版zabbix
1. docker-compose docker-compose :容器自带的编排工作,可以通过yaml编排文件,将容器要启动的命令写入文件,然后再利用docker-compose run file. ...
- 最长上生子序列LIS
学习动态规划问题(DP问题)中,其中有一个知识点叫最长上升子序列(longest increasing subsequence),也可以叫最长非降序子序列,简称LIS.简单说一下自己的心得. 我们都 ...
- 上传自己的构件(Jar)到Maven中央仓库
背景: 用了Maven之后,你有没有这样的想法,自己一直在使用别人贡献的代码,自己能不能把自己觉得好的代码也贡献出来让大家方便. 还有如果你也是一名程序员,你会不会觉得要是把自己积累起来日常常用的代码 ...