这次的题让我对选择不同数据结构所产生的结果惊呆了,一开始用的是结构来存储集合,课件上有现成的,而且我也是实在不太会,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的更多相关文章

  1. PAT 5-8 File Transfer (25分)

    We have a network of computers and a list of bi-directional connections. Each of these connections a ...

  2. File Transfer

    本博客的代码的思想和图片参考:好大学慕课浙江大学陈越老师.何钦铭老师的<数据结构> 代码的测试工具PTA File Transfer 1 Question 2 Explain First, ...

  3. File Transfer(并查集)

    题目大意:将多个电脑通过网线连接起来,不断查询2台电脑之间是否连通. 问题来源:中国大学mooc 05-树8 File Transfer (25 分) We have a network of com ...

  4. 让 File Transfer Manager 在新版本WIndows上能用

    最近研究.NET NATIVE,听说发布了第二个预览版,增加了X86支持,所以下,发现连接到的页面是:https://connect.microsoft.com/VisualStudio/Downlo ...

  5. 05-树8 File Transfer

    并查集 简单并查集:输入N,代表有编号为1.2.3……N电脑.下标从1开始.初始化为-1.合并后根为负数,负数代表根,其绝对值代表个数. We have a network of computers ...

  6. 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 ...

  7. Trivial File Transfer Protocol (TFTP)

    Assignment 2The Trivial File Transfer Protocol (TFTP) is an Internet software utility fortransferrin ...

  8. 05-树8 File Transfer (25 分)

    We have a network of computers and a list of bi-directional connections. Each of these connections a ...

  9. 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 ...

随机推荐

  1. 解决点击a标签返回页面顶部的问题

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. input上传图片预览

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  3. (14)odoo加载机制

    Odoo的启动通过openerp-server脚本完成,它是系统的入口. 然后加载配置文件openerp-server.conf 或者 .openerp_serverrc: openerp-serve ...

  4. 84. Largest Rectangle in Histogram *HARD* -- 求柱状图中的最大矩形面积

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  5. Java List的深度克隆

    关于java List的深度克隆 List是java容器中最常用的顺序存储数据结构之一.有些时候我们将一组数据取出放到一个List对象中,但是可能会很多处程序要读取他或者是修改他.尤其是并发处理的话, ...

  6. Xcode中的几个常用文件路径

    在iOS开发中有时候需要知道一些文件的路径,这里总结如下: 路径查找第一步如图: 1.模拟器的路径:/Applications/Xcode.app/Contents/Developer/Platfor ...

  7. AngularJS开发经验(转)

    AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了.所以我做了一些工作(你也可以觉得是小花招)来让 ...

  8. POC测试——原型验证,降低风险,IT系统销售工作之一

    POC测试,即Proof of Concept,是业界流行的针对客户具体应用的验证性测试,根据用户对采用系统提出的性能要求和扩展需求的指标,在选用服务器上进行真实数据的运行,对承载用户数据量和运行时间 ...

  9. RelativeLayout练习

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  10. 转: Jsp9个内置对象详解

    1.request对象 客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求, 然后做出响应.它是HttpServletRequest类的实例. 序号方法说明 objectgetA ...