这次的题让我对选择不同数据结构所产生的结果惊呆了,一开始用的是结构来存储集合,课件上有现成的,而且我也是实在不太会,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. innodb_fast_shutdown中值为1或者2的区别是?

    innodb_fast_shutdown=0 , 1 , 2时的意思分别是 0 把buffer pool中的脏页刷到磁盘和合并insert buffer,当然包括redo log也会写到磁盘中. 2 ...

  2. JavaScript开发者常忽略或误用的七个基础知识点

    JavaScript 本身可以算是一门简单的语言,但我们也不断用智慧和灵活的模式来改进它.昨天我们将这些模式应用到了 JavaScript 框架中,今天这些框架又驱动了我们的 Web 应用程序.很多新 ...

  3. 工作日志 jquery slideDown slideUp

    jquery里面使用 slideDown 和  slideUp会有一个像素的偏差

  4. SQL数据库第一部分

    数据库:程序用来存取数据的 ACCESS:自带,比较小,不是很专业 SQL Server:主要用在.NET语言中,比较专业.微软开发 MYSQL:主要用在PHP语言中,比SQL server体积比较小 ...

  5. JFrame面板

    1.可见性与透明性 可见性:当面板不可见时,则该面板中包含的组件会无法显示. 透明性:若该面板是可见且透明的,那么只是面板会透明(比如面板的背景色无法看到),面板上的组件仍会显示. 注:可见性通过se ...

  6. poj1651

    Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7252   Accepted:  ...

  7. [工程备案]linux基本命令以及C和C++编程

    whereis 查看程序安装在了哪里 where 产看运行程序在哪里. libtool --mode=link  g++ test.cpp  -o test  libSegmentorForSim2T ...

  8. [转]Perfmon - Windows 自带系统监测工具

    以下内容转自:http://blog.csdn.net/oscar999/article/details/7918385 ---------------------------分割线--------- ...

  9. Hive的Transform功能

    Hive的TRANSFORM关键字提供了在SQL中调用自写脚本的功能,适合实现Hive中没有的功能又不想写UDF的情况.例如,按日期统计每天出现的uid数,通常用如下的SQL SELECT date, ...

  10. 一个漂亮灵活的PHP图片验证码

    <?php class Imagecode{ private $width ; private $height; private $counts; private $distrubcode; p ...