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,代表没有父节点.为了使树更加平衡,可以让每一个连通分量的树根的负值代表这个连通分量 ...
随机推荐
- Windows下Apache应用环境塔建安全设置(目录权限设置)
目的:为Apache,php配置受限制的用户权限.保护系统安全.需要的朋友可以参考下. 环境配置情况: apache安装目录:d:\www-s\apache php目录:d:\www-s\php5 m ...
- php数组与字符串转换
1.将字符串转换成数组的几个函数: (1)explode(separate,string) 示例:$str = "Hello world It's a beautiful day" ...
- Xml文档数据提取到Excel表中
近期,财务一位同事,吐槽:<某XX开票软件>导出数据文档只有Xml格式,竟然没有Excel文档,工作起来非常不方便,希望我想想办法.上图: 需求分析:Xml数据----> 提取到Da ...
- Git之fatal: remote origin already exists
文件提交到远程分支,我们需要提前表明需要提交到哪个远程分支 比如:git remote add origin git@github.com:wqk66/test.git,表示他提交到远程仓库test ...
- 【转】一个Java对象到底占多大内存?
最近在读<深入理解Java虚拟机>,对Java对象的内存布局有了进一步的认识,于是脑子里自然而然就有一个很普通的问题,就是一个Java对象到底占用多大内存? 在网上搜到了一篇博客讲的非常好 ...
- webapi之fiddler头设置
Host: localhost:16648Connection: keep-aliveContent-Length: 36Accept: application/json, text/javascri ...
- Centos 6 安装python2.7.6
centos 是自带python的.但是版本稍微旧一些.搞python开发,肯定要用新一点的稳定版.所以,要升级一下python. 先去python主站下载python的源码包:Python-2.7. ...
- [译] 用win7自带工具找出svchost.exe的CPU使用率达到100%的元凶
本文是我对自己上一篇转载的博客 <Figuring out why my SVCHOST.EXE is at 100% CPU without complicated tools in Wind ...
- qobject_cast
void QLadderDiagramItem::GetMainForm(DoType sourceType){ for each (QWidget *w in qApp->topLevelWi ...
- 查看本机的ip地址
ifconfig可以查看本机的ip地址:inet addr:10.108.104.185