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. 别了,DjVu!

    作者:马健邮箱:stronghorse_mj@hotmail.com发布:2010.05.21 目录一.DjVu技术二.掌握DjVu技术的人三.玩DjVu的人四.小结跋:我与DjVu 谨以此文纪念与D ...

  2. 【转载】在AspNetCore 中 使用Redis实现分布式缓存

    原文地址:https://www.cnblogs.com/szlblog/p/9045209.html AspNetCore 使用Redis实现分布式缓存 上一篇讲到了,Core的内置缓存:IMemo ...

  3. Service Fabric 群集在Service Replica过多的情况下报错问题

    首先 Service Fabric 群集是正常的,部署一些服务过后也能正常运行,但一旦部署的服务过多后,且每个服务不止一个Partition,就有可能让群集状态为Error,但其实服务还是在正常运行的 ...

  4. 洛谷 P3586 [POI2015]LOG

    P3586 [POI2015]LOG 题目描述 维护一个长度为n的序列,一开始都是0,支持以下两种操作:1.U k a 将序列中第k个数修改为a.2.Z c s 在这个序列上,每次选出c个正数,并将它 ...

  5. python for i in range(n,m)注意...

    for i in range(n,m) 区间包含n不含m

  6. kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  7. [转]Groovy One Liners to Impress Your Friends

    Link:http://arturoherrero.com/2011/06/04/10-groovy-one-liners-to-impress-your-friends/ I find that c ...

  8. [JavaScript]如何实现一个JS脚本能在browser和NodeJs里都是用

    下面是一个Common的Solution 'use strict'; // Universal Module Definition (UMD) to support AMD, CommonJS/Nod ...

  9. Rhino-- JavaScript

    Rhino是JDK1.6里自带的一个Javascript Engine,不过他的书写风格好像有点不是很一样. importPackage(Packages.groovy.util); var o = ...

  10. Github命令git status

    输入git status可以告诉我们三件事: 1.你当前的本地库位于哪个分支上: 2.你当前的本地库和远程库的区别:它会提示本地库和远程库差了还是多了多少个提交(commit),并给你提建议,要不要p ...