04-树5. File Transfer (25)

时间限制
150 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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<=N<=104), 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<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
int f[];
int findfa(int a){
if(f[a]!=a){
f[a]=findfa(f[a]);
}
return f[a];
}
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n;
//memset(l,-1,sizeof(l));
int i,a,b,fa,fb;
char c;
scanf("%d",&n);
for(i=;i<=n;i++){
f[i]=i;
}
for(i=;;i++){
cin>>c;
if(c=='S'){
break;
}
cin>>a>>b;
fa=findfa(a);
fb=findfa(b);
if(c=='I'){
if(fa!=fb){
if(fa>fb){
f[fb]=fa;
}
else{
f[fa]=fb;
}
}
}
else{
if(fa==fb){
printf("yes\n");
}
else{
printf("no\n");
}
}
}
int num=;
for(i=;i<=n;i++){
if(f[i]==i){
num++;
}
}
if(num==){
printf("The network is connected.\n");
}
else{
printf("There are %d components.\n",num);
}
return ;
}

pat04-树5. 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. 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 ...

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

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

  6. File Transfer(并查集)

    题目大意:将多个电脑通过网线连接起来,不断查询2台电脑之间是否连通. 问题来源:中国大学mooc 05-树8 File Transfer (25 分) We have a network of com ...

  7. File Transfer

    本博客的代码的思想和图片参考:好大学慕课浙江大学陈越老师.何钦铭老师的<数据结构> 代码的测试工具PTA File Transfer 1 Question 2 Explain First, ...

  8. 让 File Transfer Manager 在新版本WIndows上能用

    最近研究.NET NATIVE,听说发布了第二个预览版,增加了X86支持,所以下,发现连接到的页面是:https://connect.microsoft.com/VisualStudio/Downlo ...

  9. PAT 05-树7 File Transfer

    这次的题让我对选择不同数据结构所产生的结果惊呆了,一开始用的是结构来存储集合,课件上有现成的,而且我也是实在不太会,150ms的时间限制过不去,不得已,看到这题刚好可以用数组,结果7ms最多,有意思! ...

随机推荐

  1. Hyper-V 中遇到错误 'vm' could not initialize

    不知道前面做了什么操作,但当我启动我的虚拟机时遇到了'vm' could not initialize的错误,如下图: 在几番尝试未果后,找到了如下解决方法: 以管理员模式启动cmd,执行命令 net ...

  2. C#实现截图

    语言环境 框架: .NET Framework 3.5IDE: VS2013窗体A(主窗体) /// <summary>/// 点击弹出截屏窗体/// </summary>// ...

  3. ubuntu - 安装软件问题

    problem & solution 问题1 - E: 无法定位软件包 @原因(1) - 没有添加相应软件的镜像源(软件源)    解决方案 用 gedit/vi/vim - 在 /etc/a ...

  4. Linux之创建777权限的文件

    服务器中运行项目的时候,有时候会出现图片上传失败,查看报错原因才知道是文件夹没有写入权限导致上传失败. 方案1: 在使用Linux命令更改对应目录的权限 方案2: 在代码中创建文件夹的时候给予对应的7 ...

  5. [翻译]CURAND Libaray--Host API--(1)

    原文来自:cuda curand toolkit document Translated by xingoo 如果有错误请联系:xinghl90@gmail.com 2Host API简述 使用hos ...

  6. db2联邦数据库

    目标机器:192.168.0.16 本地机器:192.168.0.18 .登陆本地数据库 db2 connect to dwmm user dainst using dainst ## 打开联邦数据库 ...

  7. 拓扑排序+数学+DP【洛谷P1685】 游览

    P1685 游览 题目描述 顺利通过了黄药师的考验,下面就可以尽情游览桃花岛了! 你要从桃花岛的西头开始一直玩到东头,然后在东头的码头离开.可是当你游玩了一次后,发现桃花岛的景色实在是非常的美丽!!! ...

  8. 10.17(山东多校联合模拟赛 day1)

    山东多校联合模拟赛 day1 题不难 rect [问题描述] 给出圆周上的 N 个点, 请你计算出以这些点中的任意四个为四个角,能构成多少个矩形. 点的坐标是这样描述的, 给定一个数组 v[1..N] ...

  9. There is no row in position 0

    更改程序池 管道模式 ---->经典    常见设置问题: 32位启用

  10. C语言关于++i,--i,i++,i--

    ++i 和--i 指的是先进行运算,再进行调用(运算符在前) i++和i--指的是先进行调用,再进行运算(运算符在后) 举例: int k,i=5;k=i++;//k得到5i=5;k=++i;//k得 ...