这次的题让我对选择不同数据结构所产生的结果惊呆了,一开始用的是结构来存储集合,课件上有现成的,而且我也是实在不太会,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. iOS高性能图片架构与设计

    版权声明:本文由柯灵杰原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/157 来源:腾云阁 https://www.qclo ...

  2. Linux命令行下编译Android NDK的示例代码

    这几天琢磨写一个Android的Runtime用来加速HTML5 Canvas,让GameBuilder+CanTK 不但开发速度快,运行速度也能接近原生应用.所以花了点时间研究 Android ND ...

  3. Controller简介

    Controller控制器,是MVC中的部分C,为什么是部分呢?因为此处的控制器主要负责功能处理部分: 1.收集.验证请求参数并绑定到命令对象: 2.将命令对象交给业务对象,由业务对象处理并返回模型数 ...

  4. IE和主流浏览器

    1.添加事件 addEventListener 主流 attachEvent     IE 2.移除事件 removeEventListener detachEvent 3.获取事件对象 event ...

  5. 5.6 WebDriver API实例讲解(16-30)

    16.操作单选框 被测试的网页为Demo1. Java语言版本的API实例代码: public static void operateRadio(){ driver.get("file:// ...

  6. JavaScript 3D图表

    在说3D图表以前,首先要明确两个概念,一个是数据的维度,一个是呈现数据载体的维度.对于数据的维度,一维的数据呈现,但是呈现的载体是二维的平面图,比如饼图: 已经能够很清晰地观察到数据的分布情况.数据如 ...

  7. (24)odoo中模型标识汇总

    * 设置->技术->数据结构->模型                模型    模型描述    类型    瞬态模型account.account    科目    基础对象    ...

  8. mysql 初始密码 设置

    mysql root 密碼的設置方法 shell> mysql -u root mysql mysql> SET PASSWORD FOR root@localhost=PASSWORD( ...

  9. Android receiver

    可以在代码文件中声明一个receiver,也可以在manifest中声明一个,前者中的receiver只有在该activity launch起来以后才会监听其所感兴趣的事件, 而如果在androidM ...

  10. poj3159 Candies(差分约束,dij+heap)

    poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...