PAT 05-树7 File Transfer
这次的题让我对选择不同数据结构所产生的结果惊呆了,一开始用的是结构来存储集合,课件上有现成的,而且我也是实在不太会,150ms的时间限制过不去,不得已,看到这题刚好可以用数组,结果7ms最多,有意思!什么是时间复杂度,终于有了一次感性的认识。这题还有个check的要求,我用了一个链表来存储,感觉挺麻烦的,不知是否有更好的方法,题设要求及代码实现如下
/*
Name:
Copyright:
Author:
Date: 06/04/15 09:46
Description:
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 <stdio.h>
#include <stdlib.h>
#include <stdbool.h> typedef struct Flag
{
bool flag;
struct Flag * next;
}Flag, * pFlag; int Find(int S[], int X);
void Union(int S[], int X1, int X2); int MaxSize; int main()
{
// freopen("in.txt", "r", stdin); // for test
int i, c1, c2, k;
char ch; scanf("%d\n", &MaxSize); int S[MaxSize];
for(i = ; i < MaxSize; i++)
S[i] = -; pFlag head = (pFlag)malloc(sizeof(Flag));
pFlag p, tmp; p = head;
scanf("%c", &ch);
while(ch != 'S')
{
if(ch == 'C')
{
pFlag f = (pFlag)malloc(sizeof(Flag));
f->next = NULL;
scanf("%d%d\n", &c1, &c2);
if(Find(S, c1) == Find(S, c2))
f->flag = true;
else
f->flag = false;
p->next = f;
p = p->next;
}
else
{
scanf("%d%d", &c1, &c2);
Union(S, c1, c2);
}
scanf("%c", &ch);
} p = head->next;
tmp = head;
free(tmp);
while(p)
{
if(p->flag)
printf("yes\n");
else
printf("no\n");
tmp = p;
p = p->next;
free(tmp);
}
k = ;
do
{
if(S[--MaxSize] < )
k++;
}while(MaxSize);
if(k > )
printf("There are %d components.\n", k);
else
printf("The network is connected.\n");
// fclose(stdin); // for test
return ;
} int Find(int S[], int X)
{
X--;
for(; S[X] >= ; X = S[X]); return X;
} void Union(int S[], int X1, int X2)
{
int Root1, Root2; Root1 = Find(S, X1);
Root2 = Find(S, X2);
if(Root1 != Root2)
{
if(S[Root1] <= S[Root2])
{
S[Root1] += S[Root2];
S[Root2] = Root1;
}
else
{
S[Root2] += S[Root1];
S[Root1] = Root2;
}
}
}
PAT 05-树7 File Transfer的更多相关文章
- PAT 5-8 File Transfer (25分)
We have a network of computers and a list of bi-directional connections. Each of these connections a ...
- File Transfer
本博客的代码的思想和图片参考:好大学慕课浙江大学陈越老师.何钦铭老师的<数据结构> 代码的测试工具PTA File Transfer 1 Question 2 Explain First, ...
- File Transfer(并查集)
题目大意:将多个电脑通过网线连接起来,不断查询2台电脑之间是否连通. 问题来源:中国大学mooc 05-树8 File Transfer (25 分) We have a network of com ...
- 让 File Transfer Manager 在新版本WIndows上能用
最近研究.NET NATIVE,听说发布了第二个预览版,增加了X86支持,所以下,发现连接到的页面是:https://connect.microsoft.com/VisualStudio/Downlo ...
- 05-树8 File Transfer
并查集 简单并查集:输入N,代表有编号为1.2.3……N电脑.下标从1开始.初始化为-1.合并后根为负数,负数代表根,其绝对值代表个数. We have a network of computers ...
- Cordova Upload Images using File Transfer Plugin and .Net core WebAPI
In this article, I am going to explain ,"How to Upload Images to the server using Cordova File ...
- Trivial File Transfer Protocol (TFTP)
Assignment 2The Trivial File Transfer Protocol (TFTP) is an Internet software utility fortransferrin ...
- 05-树8 File Transfer (25 分)
We have a network of computers and a list of bi-directional connections. Each of these connections a ...
- SSH File Transfer遇到错误"too many authentication failures for root".A protocol error was detected......
在SSH Secure Shell 连接Linux centos的时候,遇到F-Secure SSH File Transfer错误"too many authentication fai ...
随机推荐
- 在IIS7.5打开网页的时候,提示: HTTP 错误 500.0 - Internal Server Error 调用 LoadLibraryEx 失败,在 ISAPI 筛选器 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\\aspnet_filter.dll" 上。解决方法
- 【bzoj1052】覆盖问题
[bzoj1052]覆盖问题 分析 考虑二分\(L\)的值,然后判断3个\(L*L\)能否覆盖所有的点. 这时候出现了两种可能的思路. 思路1 首先,3是一个很小的常数. 我们想:假如能探究出1和2的 ...
- Java集合框架:HashMap
转载: Java集合框架:HashMap Java集合框架概述 Java集合框架无论是在工作.学习.面试中都会经常涉及到,相信各位也并不陌生,其强大也不用多说,博主最近翻阅java集合框架的源码以 ...
- jq实现某个标签内,达到一定字数后,剩下的用 ... 显示
$(".infom_con").each(function(){ var text=$(this).find("a").text(); var len=text ...
- 谈谈JPA-01-概述
JPA和Hibernate的关系 JPA 是 hibernate 的一个抽象(就像JDBC和JDBC驱动的关系): JPA 是规范:JPA 本质上就是一种 ORM 规范,不是ORM 框架 —— 因为 ...
- DSP EPWM学习笔记1 - EPWM定时中断
DSP EPWM学习笔记1 - EPWM定时中断 彭会锋 EPWM模块组成 EPWM有7个子模块组成:时间基准 TB.比较功能 CC.动作限定 AQ.死区产生 DB.斩波控制 PC.故障捕获 TZ.事 ...
- Objective-C 中 NULL、nil、Nil、NSNull 的定义及不同
本文由我们团队的 康祖彬 童鞋撰写,这是他的个人主页:https://kangzubin.cn. 理解"不存在"的概念不仅仅是一个哲学的问题,也是一个实际的问题.我们是有形宇宙的居 ...
- java配置数据库连接池的方法步骤
java配置数据库连接池的方法步骤 java配置数据库连接池的方法步骤,需要的朋友可以参考一下 先来了解下什么是数据库连接池数据库连接池技术的思想非常简单,将数据库连接作为对象存储在一个Vecto ...
- HDU 4123(树的直径+单调队列)
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 我的Github注册使用之旅
[个人介绍] 我是来自网络工程143班的姜金金,学号是1413042066.我没什么大的爱好,闲时喜欢在有阳光的午后喝喝小茶,捧一本书慢慢品茗:也喜欢散散步,欣赏细碎事物的美好,驻足沿路美丽的风景.说 ...