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. 3、Vue实例的属性

    1.获取Vue实例的属性 2.data属性 每个Vue实例都会代理其data对象里所有的属性.如果实例创建之后添加或者更改属性,他不会触发视图更新. 这句话说了下面两件事情 1.每个Vue实例都会代理 ...

  2. selenium中元素操作之上传操作(六)

    上传操作分为两种情况: 1.input标签上传 如果是input可以直接输入路径的,那么直接调用send_keys输入路径,和前边的元素操作类似,在这里不再过多的赘述. 2.非input标签上传 非i ...

  3. 八.软件自动化和web测试

    1.软件自动化测试 1.1  自动化测试的概念   自动化测试:就是通过测试工具或其他手段,按照测试工程师的预定计划对软件产品进行自动化的测试   软件测试自动化涉及到测试流程.测试体系.自动化编译以 ...

  4. 下一代微服务-ServiceMesh

    1.简介 系统服务化之后,服务间通信需要关注什么? 服务发现.负载均衡.路由.流控.通信可靠性.弹性.安全.监控.日志 API网关可以集中式的管理这些功能,但是会出现单点故障,并且实现起来网关会变得越 ...

  5. mysql.cnf配置文件详解

    参数详解 [client] #客户端设置,即客户端默认的连接参数port = 3307   #默认连接端口socket = /data/mysqldata/3307/mysql.sock #用于本地连 ...

  6. es聚合后排序

    注意: es版本至少6.1以上 语句: GET 76/sessions/_search { "size": 0, "query": { "bool&q ...

  7. Django 初始化数据库遇到问题(python manage.py migrate)

    问题:django.db.utils.InternalError: (1049, "Unknown database 'main'") 在Django 配置的数据库上 执行  cr ...

  8. SpringCloud学习第二章-SpringBoot

    SpringCloud 学习前提     SpringCloud是基于SpringBoot构建的,因此他延续了SpringBoot的契约模式以及开发方式.下面将讲到SpringBoot的构建方式. S ...

  9. JavaScript一些对象。

    Function对象: 1.创建方式: function f(a) { alert(a); } var f=function(a){ alert(a); } 2.方法: 3.属性: 1.length: ...

  10. python生成图片二维码(利用pillow)

    首先 pip install pillow 然后 from PIL import Image from PIL import ImageDraw from PIL import ImageFont i ...