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 分)的更多相关文章

  1. 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 ...

  2. PAT 5-8 File Transfer (25分)

    We have a network of computers and a list of bi-directional connections. Each of these connections a ...

  3. 05-树8 File Transfer (25 分)

    We have a network of computers and a list of bi-directional connections. Each of these connections a ...

  4. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

  5. pat04-树5. File Transfer (25)

    04-树5. File Transfer (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue We have ...

  6. L2-006 树的遍历 (25 分)

    链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 题目: 给定一棵二叉树的后序遍历和中序 ...

  7. 7-3 树的同构(25 分) JAVA

    给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的. 例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后,就得到另外一棵树 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 学习python及Pygame的安装及运行

    Python: 注意勾上Add Python 2.7 to PATH,然后点“Install Now”即可完成安装. 或手动修改环境变量,win7:右击我的电脑->属性->高级->环 ...

  2. NLog使用整理

    NLog使用中碰到的问题整理 1,日志写mysql数据库报错, 原因: 在sql语句中使用了mysql的函数now() 导致插入失败, 解决办法: 使用参数代替now(). 在nlog配置文件中设置  ...

  3. JS中的加减乘除和比较赋值

    隐式类型转换 使用Boolean(), Number(), String()去转换数据类型的方法叫显示类型转换,而与它相对的就是隐式类型转换,隐式类型转换并没有明显的标志,而是JS解释器觉得做这样一个 ...

  4. c# 多线程的几种方式

    1.什么是线程? 进程作为操作系统执行程序的基本单位,拥有应用程序的资源,进程包含线程,进程的资源被线程共享,线程不拥有资源. 2.前台线程和后台线程的区别? 程序关闭时,后台线程直接关闭,但前台线程 ...

  5. DAY5:字典

    无序性: # -*- coding:utf-8 -*- # Author: TanJincheng room = { "s2": "han meimei", & ...

  6. jmeter保持登录

    1.添加配置元祖---http cookie 管理器(注意上面的http cookie管理器和其他的配置是对其的) 2.这里是当你想要配置多用户并发是要配置不同的账号,配置后记得上面的第一张图的清除c ...

  7. Groovy学习笔记-动态类型

    1.respondsTo方法判断对象是否存在指定方法 interface IHelp{ void helpMoveThings() } class Man implements IHelp{ void ...

  8. JAVAEE期末项目------文章发布系统

    项目文档和代码的GitHub地址:https://github.com/xiangbaobaojojo/- 1.项目介绍 (计科四班  蔡春燕 20150141401)和我 陈香宇(计科四班  201 ...

  9. [转载] Fiddler为所欲为第三篇 封包逆向必备知识[三]

    导语: 小A同学:会抓包有什么好学习的,不就用一个工具设置个wifi代{过}{滤}理就OK了嘛,还不是很多做不了.不如学习安卓逆向.didi科学家:不好意思,抓包可以为所欲为. 其实学习抓包,完完全全 ...

  10. SVN配置(服务器端及客户端)

    版本控制工具,在前几年一直用的SVN,从去年后半年开始本人开始使用GIT.为了避免后期再次使用SVN过程中出现生疏,在此将SVN服务端及客户端配置方式罗列出来. 本人使用的SVN版本为 服务端: Vi ...