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,代表没有父节点.为了使树更加平衡,可以让每一个连通分量的树根的负值代表这个连通分量 ...
随机推荐
- python staticmethod&classmethod
python中的这两种方法都通过修饰器来完成 静态方法: 不需要传递类对象或者类的实例 可以通过类的实例.方法名a().foo()或者类名.方法a.foo()名来访问 当子类继承父类时,且实例化子类时 ...
- 在线编译器Coding Ground
http://www.tutorialspoint.com/codingground.htm Free Online IDE and Terminal - Edit, Compile, Execute ...
- P2667 超级质数
https://www.luogu.org/problem/show?pid=2667 题目背景 背景就是描述,描述就是背景...... 题目描述 一个质数如果从个位开始,依次去掉一位数字,两位数字, ...
- Winform datagridview 基础
======================================================================================== == 重点需要掌握 A ...
- python2和python3的区别(转)
基本语法差异 核心类差异 Python3对Unicode字符的原生支持 Python2中使用 ASCII 码作为默认编码方式导致string有两种类型str和unicode,Python3只支持uni ...
- HTML的历史与历史遗留问题
1. <style type="text/css"> 从前,HTML的设计者认为以后应该还会有其他样式,不过如今我们已经醒悟,事实表明,完全可以只使用<style ...
- vs2010 在函数级别设置优化
平时开发的时候,为了方便调试,visual studio 的Configuration 设置成Release. 同时为了事后调试,Optimization总是设置成Disabled.这样做是方便查看变 ...
- Servlet 3.0 新特性详解 (转载)
原文地址:https://www.ibm.com/developerworks/cn/java/j-lo-servlet30/ Servlet 3.0 新特性概述 Servlet 3.0 作为 Jav ...
- [Redis] 基于redis的分布式锁
前言分布式锁一般有三种实现方式:1. 数据库乐观锁:2. 基于Redis的分布式锁:3. 基于ZooKeeper的分布式锁.本篇博客将介绍第二种方式,基于Redis实现分布式锁. 可靠性首先,为了确保 ...
- Vue之computed与watch的使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...