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. Android proguard代码混淆

    为什么要代码混淆? Android的安装文件是apk格式.APK是AndroidPackage的缩写.是由android sdk编译的工程打包生成的安装程序文件. Apk其实是zip文件,但是后缀名被 ...

  2. linux linux系统常用设置

    linux  linux系统常用设置 一.设置开机时开启数字键 修改rc.local文件 命令:vi  /etc/rc.local rc.local文件中增加如下代码: INITTY=/dev/tty ...

  3. Kotlin第一篇 Hello Kotlin以及简单介绍。

    首先需要一个编译器,我们使用Intellij IDE  https://www.jetbrains.com/idea/download/#section=windows 下载下来安装好. 那么我们就来 ...

  4. 基于pythpn的深度学习 - 记录

    [基于pythpn的深度学习] 环境:    windows/linux-ubuntu    Tensorflow (基于anaconda)        *安装 (python3.5以上不支持)   ...

  5. Python 利用循环画散点图

    import pandas as pd data = pd.read_csv('D:/suning/iris.csv') data = data.iloc[:,1:] ###2维散点图 import ...

  6. THINKPHP 框架的模板技术

    //echo C('name'); App/Action/IndexAction.class.php文件夹下的 URL模式 //输出URL模式//echo C('URL_MODEL'),'<br ...

  7. ARC102D(构造)

    ARC102D(构造) 构造一个图,使得\(n \le 20,m\le 60\),边从小的点连向大的点,并且从1到n的所有路径,长度与\([0, l-1]\)构成双射. 用二进制的思想--代码很鬼畜 ...

  8. springboot整合mybatis,redis,代码(四)

    一 说明 这是spring整合redis注解开发的系类: 二 正文 在注解开发时候,会有这几个注解需要注意: 具体含义: 1.@Cacheable 可以标记在方法上,也可以标记在类上.当标记在方法上时 ...

  9. Forward链、Input链和Output链的区别

    转载自:http://blog.chinaunix.net/uid-27863080-id-3442374.html 1. 如果数据包的目的地址是本机,则系统将数据包送往Input链.如果通过规则检查 ...

  10. java 在web应用中获取本地目录和服务器上的目录不一致的问题

    先来讲讲我所遇到的问题.最近有个新的项目添加新的功能. 修改之后部署到服务器上面发现取到classpath目录跑到别的地方去了.在本地测试却正常. 当时毛的着火了.硬是想不懂什么问题. 终于发现了这个 ...