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

  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. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

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

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

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

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

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

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

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

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

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

  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. 关于MVC与三层架构

    详情 回答一: 当然啊,你要明白三层架构的MVC的区别和联系: 三层架构是最基本的项目分层结果,而MVC则是三层架构的一个变体,MVC是一种好的开发模式.首先你要明白MVC分别代表的是什么意思. M  ...

  2. xml文件操作帮助类

    xml文件的节点值获取和节点值设置 /// <summary> /// 保存单个点节点的值 /// </summary> /// <param name="Up ...

  3. javascript异步编程学习及实例

    所谓异步就是指给定了一串函数调用a,b,c,d,各个函数的执行完结返回过程并不顺序为a->b->c->d(这属于传统的同步编程模式),a,b,c,d调用返回的时机并不确定. 对于js ...

  4. 2019 东方财富网java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.东方财富网等公司offer,岗位是Java后端开发,因为发展原因最终选择去了东方财富网,入职一年时间了,也成为 ...

  5. v2 配置

    记录一下怕自己忘记了 v2配置 v2加速 cdn加速 hostwind 服务器 namesilo 域名网站

  6. Mybatis映射器接口代理对象的方式 运行过程

    查询一张表的所有数据. 环境: 使用工具IntelliJ IDEA 2018.2版本. 创建Maven工程不用骨架 1.pom.xml <?xml version="1.0" ...

  7. 很带劲,Android9.0可以在i.MX8开发板上这样跑

    米尔MYD-JX8MX开发板移植了Android9.0操作系统,现阶段最高版本的Android9.0操作系统将给您的产品在安全与稳定性方面带来更大的提升.可惜了,这里不能上传视频在i.MX8开发板跑A ...

  8. java web的转发与重定向

    java web的转发与重定向 原创 2012年12月15日 11:51:39 19312 转发和重定向的区别 一.使用方法 转发:request.getRequestDispatcher(" ...

  9. Python之路(第四十一篇)线程概念、线程背景、线程特点、threading模块、开启线程的方式

    一.线程 ​ 之前我们已经了解了操作系统中进程的概念,程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别就在于:程序是指令的集合,它是 ...

  10. MySQL与Oracle的隔离级别

    一.事务和隔离级别 事务的概念:事务是把对数据库的一系列操作都看做一个整体,要么全部成功,要么全部失败,利用事务我们可以保证数据库的完整性,事务具有原子性. 隔离级别:隔离级别定义了事务与事务之间的隔 ...