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 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 NN (2\le N\le 10^42≤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 NN. 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.
这题是考并查集
/*
运行详情
评测结果
时间 结果 得分 题目 编译器 用时(ms) 内存(MB) 用户
2017-06-29 18:12 部分正确 24 5-8 gcc 27 1
测试点结果
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 7/7 18 1
测试点2 答案正确 7/7 1 1
测试点3 答案正确 1/1 2 1
测试点4 答案正确 1/1 2 1
测试点5 答案正确 4/4 14 1
测试点6 答案正确 4/4 15 1
测试点7 答案错误 0/1 27 1 没有ac,不知道最后一个测试点用了什么数据
*/ #include<stdio.h>
#define MAXLEN 15000
#define TOPLEVEL -1
int gComputers[MAXLEN];
void InitComputers(int n)
{
int i;
for(i=1;i<=n;i++)
{
gComputers[i]=TOPLEVEL;
}
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void Connect(int a,int b)
{
/* if(gComputers[a]==TOPLEVEL)
{
gComputers[b]=a;
return;
}
*/
int temp;
temp=a;
while(gComputers[temp]!=TOPLEVEL)
{
temp=gComputers[temp];
}
// gComputers[a]=temp;
gComputers[b]=temp;
return;
} /*
int GetTop(int a)
{ int temp=a;
if(gComputers[a]==TOPLEVEL)
return a; while(gComputers[temp]!=TOPLEVEL)
temp=gComputers[temp];
gComputers[a]=temp; //查找完了合并父节点;
return temp; }
*/
int GetTop(int k)
{
if(gComputers[k]<0)
{
return k;
}
return gComputers[k] = GetTop(gComputers[k]);
} void Check(int a,int b)
{
if(GetTop(a)==GetTop(b))
{
printf("yes\n");
}
else
{
printf("no\n");
}
} void ShowSummry(int n)
{
int i,count=0;
for(i=1;i<=n;i++)//此处扫描应从1号开始,电脑从1开始编号,0号没操作始终是-1;
{
if(gComputers[i]==TOPLEVEL)
{
count++;
}
}
if(count==1)
{
printf("The network is connected.");
}
else
printf("There are %d components.",count);
} void OutputStatus(int n)
{
int i;
for(i=0;i<=n;i++)
{
printf("--%d:%d\n",i,gComputers[i]);
}
} int main()
{
int N;
int a,b;
char command;
scanf("%d",&N);
InitComputers(N);
while(1)
{
scanf("%c",&command);
if(command=='S')
{
ShowSummry(N);
// OutputStatus(N);
break;
} scanf("%d %d",&a,&b);
if(command=='I')
Connect(a,b);
if(command=='C')
Check(a,b);
}
}
不知道最后一个测试点什么情况,mooc开课之外的题库里面不带提示,找了一张别人做题的截图,发现最后一个点是卡不压缩的,容易超时
问题是我有一直合并路线,而且时间也不长。不知道遇上什么问题了。

PTA 05-树8 File Transfer (25分)的更多相关文章
- PAT 5-8 File Transfer (25分)
We have a network of computers and a list of bi-directional connections. Each of these connections a ...
- 05-树8 File Transfer (25 分)
We have a network of computers and a list of bi-directional connections. Each of these connections a ...
- PTA甲级1094 The Largest Generation (25分)
PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...
- L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...
- pat04-树5. File Transfer (25)
04-树5. File Transfer (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue We have ...
- PTA 04-树5 Root of AVL Tree (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/668 5-6 Root of AVL Tree (25分) An AVL tree ...
- PTA 10-排序5 PAT Judge (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/677 5-15 PAT Judge (25分) The ranklist of PA ...
- PTA 05-树7 堆中的路径 (25分)
题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/713 5-5 堆中的路径 (25分) 将一系列给定数字插入一个初始为空的小顶堆H[] ...
- pta 编程题13 File Transfer
其它pta数据结构编程题请参见:pta 这道题考察的是union-find并查集. 开始把数组中每个元素初始化为-1,代表没有父节点.为了使树更加平衡,可以让每一个连通分量的树根的负值代表这个连通分量 ...
随机推荐
- 168 Excel Sheet Column Title Excel表列名称
给定一个正整数,返回它在Excel表中相对应的列名称.示例: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -&g ...
- python中函数参数
默认参数注意点 优点:灵活,当没有指定与形参对应的实参时就会使用默认参数 缺陷: 例子: >>> def h(m, l=[]): #默认参数时列 ...
- php的iconv函数中utf8与utf-8的差异
开发中遇到一个奇怪的问题:报错如下: iconv() [<a href='function.iconv'>function.iconv</a>] : Wrong charset ...
- 猩球StarBall ,一个方便约球的小程序
扫描小程序码直接进入小程序 猩球StarBall 是一款为热爱运动的人群提供便利的小程序. 开发技术为Java +Mysql 其中用到的技术框架为SpringBoot,Mybatis,Redis,Qu ...
- SpringMvc返回@ResponseBody中文乱码
使用SpringMvc的@ResponseBody返回指定数据的类型做为http体向外输出,在浏览器里返回的内容里有中文,会出现乱码,项目的编码.tomcat编码等都已设置成utf-8,如下返回的是一 ...
- CSS进阶:提高你前端水平的 4 个技巧
译者注:随着 Node.js .react-native 等技术的不断出现,和互联网行业的创业的层出不穷,了解些前端知识,成为全栈攻城师,快速的产出原型,展示你的创意,对程序员,尤其是在创业的程序员来 ...
- iOS Getter 和Setter 注册xibcell
// 初始化cell的xib的方式 [tableView registerNib:[UINib nibWithNibName:@"LXmiddleCell" bundle:nil] ...
- python中的get函数
>>> a={1:'a',2:'b'}>>> print a.get(1)a>>> print a.get(3)None
- java (给出年月日,计算该日是该年的第n天 )
package com.ywx.testdemo01; import java.util.Scanner; /** * 题目:给出年月日,计算该日是该年的第n天 * @author yangwenxu ...
- 洛谷 P2580 于是他错误的点名开始了
题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边搓炉石一边点名以至于有一天他连续点到了某个同学两次,然后正好被路过的校长发现了然后就是一顿欧拉欧拉欧拉(详情请见已结束比赛CON900). ...