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>
#include <stdlib.h>
typedef int EleType;
const int maxn = 1e5;
int MaxSize; struct SetType{
EleType Data;
int Parent;
} s[maxn]; void init();
void merge_Search();
void judge(int n, int m);
void connect(int n, int m);
int find_root(int child);
void statistic(int maxsize); int main(){
init();
merge_Search();
statistic(MaxSize);
} void init() {
scanf("%d", &MaxSize);
for (int i=; i<MaxSize; i++) {
s[i].Data = i + ;
s[i].Parent = -;
}
} void merge_Search() {
char c;
int n, m; getchar();
scanf("%c", &c); while(c != 'S') {
scanf(" %d %d", &n, &m);
switch (c) {
case 'C':
judge(n, m);
break;
case 'I':
connect(n, m);
}
getchar();
scanf("%c", &c);
}
} void judge(int n, int m) {
if(find_root(n) == find_root(m)) printf("yes\n");
else printf("no\n");
} int find_root(int data) {
int i = data - ;
if(s[i].Parent < ) {
return i;
}
s[i].Parent = find_root(s[i].Parent + );
return s[i].Parent;
} void connect(int n, int m) {
int root1 = find_root(n);
int root2 = find_root(m);
if (root1 != root2) {
if (s[root1].Parent > s[root2].Parent) {
s[root2].Parent += s[root1].Parent;
s[root1].Parent = root2;
}
else {
s[root1].Parent += s[root2].Parent;
s[root2].Parent = root1;
}
} }
void statistic(int MAX) {
int count = ;
for (int i=; i<MAX; i++) {
if(s[i].Parent < ){
count++;
}
}
if(count == ) printf("The network is connected.\n");
else printf("There are %d components.\n", count); }
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 ...
- 05-树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的左右孩子互换后,就得到另外一棵树 ...
- 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 ...
随机推荐
- python常见函数以及模块调用
1.常用函数区别 print: 在python3.0中print是函数,这意味着需要编写print(A )而不是print A str()和repr()的区别 >>>print st ...
- Hadoop Hive HBase Spark Storm概念解释
HadoopHadoop是什么? 答:一个分布式系统基础架构. Hadoop解决了什么问题? 答:解决了大数据(大到一台计算机无法进行存储,一台计算机无法在要求的时间内进行处理)的可靠存储(HDFS) ...
- c#多线程thread实例详解
1. Thread线程启动 由于ThreadStart是一个委托,所以可以简化写法 static void Main(string[] args) { Console.WriteLine(" ...
- Redis学习笔记一
Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用. Redis不仅仅支持简单 ...
- 封装Thread的两种方法 via C++ in Linux
方法一: 代理线程函数(proxyThreadFunc)作为类的静态成员函数, 回调函数指针作为类的私有成员变量 方法二: 代理线程函数(proxyThreadFunc)作为全局函数, 回调函数指针 ...
- Python第九课学习
Python第九课学习 数据结构: 深浅拷贝 集合set 函数: 概念 创建 参数 return 定义域 www.cnblogs.com/yuanchenqi/articles/5782764.htm ...
- 3.14 unittest之skip
3.14 unittest之skip 前言当测试用例写完后,有些模块有改动时候,会影响到部分用例的执行,这个时候我们希望暂时跳过这些用例.或者前面某个功能运行失败了,后面的几个用例是依赖于这个功能的用 ...
- yarn安装
Yarn是Facebook提供的替代npm的工具,可以加速node模块的下载.React Native的命令行工具用于执行创建.初始化.更新项目.运行打包服务(packager)等任务. 先安装nod ...
- VI操作
[[ 开头]] 结尾[[^ 开头第一个字符]]$ 结尾最后一个字符 以下转载自:http://www.cnblogs.com/88999660/articles/1581524.html 进入vi的 ...
- 使用spark访问hive错误记录
在spark集群中执行./spark-shell时报以下错误: 18/07/23 10:02:39 WARN DataNucleus.Connection: BoneCP specified but ...