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 ...
随机推荐
- Asp.Net Core通过HttpStatusCode状态处理响应结果
在我的一个Asp.Net Core 2.1 的项目中,我们需要通过获得服务器返回的状态,去实现不用的操作,经过多方资料查询和实践,个人总结一种方法 一.修改控制器 在要返回值的控制器Action中,R ...
- verilog reg 初值问题
虽然没有写初值 但是硬件电路肯定有逻辑电平的如果是用fpga实现的 缺省值为全0 也可以在信号声明时指定初始值如果是asic实现 初始值是随机的(但也是某个电平) 声明时指定初值会被忽略 rtl仿真时 ...
- Java中String类型的数据比较
在Java中如果想比较两个字符串是否相等,可以使用string1==string2 或string1.equal(string2)来比较. 但是,第一种方法过于局限.例如, String string ...
- 关于手机ios和安卓和pc的点击事件的兼容
var u = navigator.userAgent, app = navigator.appVersion; var clickEvent; var isAndroid = u.indexOf(' ...
- 一道笔试题来理顺Java中的值传递和引用传递
题目如下: private static void change(StringBuffer str11, StringBuffer str12) { str12 = str11; str11 = ...
- 如何用core自动创建model,与数据库连接
打开vs.点击视图->其他->程序包管理器控制台->选择默认项目->输入 Scaffold-DbContext “Data Source=XXX;Initial Catalog ...
- 自动删除Android工程中无用的资源
开发时间久了, 几个版本迭代之后, 工程中难免留下很多垃圾资源, 造成apk的包很大, 这里介绍一个工具, 可以自动扫描工程中, 没有使用的资源, 然后自动删除: 包括图片, xml, 文本等. 采用 ...
- C#经典面试题及答案
1.请你说说.net 中类和结构的区别? 答:结构和类具有大体的语法,但是结构受到的限制比类多.结构不能声明默认的的构造函数,为结构的副本是编译器创建和销毁的,所以不需要默认的构造函数和析构函数,结构 ...
- vim撤销与反撤销
按esc进入一般模式之后,u按键则为撤销上一步操作,ctrl+r则为反撤销操作
- Window 下载Android系统源代码
第一步:使用命令下载manifest文件夹 git clone https://aosp.tuna.tsinghua.edu.cn/ 第二步:进入文件夹,打开default.xml文件并找到自己要下载 ...