数据结构习题集-4-2 集合的运用

1.题目:

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. 2.题目分析:
其实这个题目感觉比4-1要更简单一些,AVL二叉树反而自己更难一些。
我使用一个结构来来描述每一个节点,因为输入的节点就是X,所以我觉得没有必要在这个结构中定义data。直接用输入的X-1指向数组中的节点就可以了。
每一个结构中定义一个parent,指向父节点的index。
查找父节点就变得很简单了,直接从A[x-1],跟链表遍历一样一直追溯到parent < 0为止。(我们约定如果parent<0,则说明这个节点是父节点)
插入则更简单,查找两个节点的父节点,如果相同,退出,如果不同,则说明父节点不同。将一个节点的父节点指向另一个节点的父节点即可。
最后,遍历所有的节点,统计有多少节点的父节点是小于0的,统计节点个数。
按规定输出。 不过,很莫名的发现了有一个测试条件超时。突然觉得按照set的元素数字大小比对后进行union后才可以。
所以在合并的时候,比较parent值得大小,如果将parent的绝对值比较大的那一个作为父节点,将小parent加入父parent,累积元素个数。
然后非常完美的通过了。

源代码如下:
 #include <stdio.h>

 int MaximumSize;

 typedef struct setNode{
// int data;
int parent;
}tSet; int FindSet(tSet a[] , int x)
{
// i is the index of x;
int i = x - ;
if(x > MaximumSize)
{
return -;
}
for(;(a[i].parent)>=;i=a[i].parent)
{
if(i>=MaximumSize)
return -;
}
return i;
} int UnionSet(tSet a[], int x, int y)
{
int px = FindSet(a,x);
int py = FindSet(a,y);
if(px==- || py ==-)
{
return -;
}//if any of the xy is not in the set;
if(px!=py)
{
if(a[px].parent < a[py].parent)
{
a[px].parent = a[px].parent + a[py].parent;
a[py].parent = px;
}
else
{
a[py].parent = a[px].parent + a[py].parent;
a[px].parent = py;
}
// a[px].parent = py;
}
return ;
} int SetNum(tSet a[])
{
int count = ;
int i = ;
for(;i<MaximumSize;i++)
{
if(a[i].parent < )
{
count++;
}
}
return count;
} //tSet* SetInitial()
//{
// tSet *a = malloc(sizeof(tSet)*MaximumSize);
// int i=0;
// for(;i<MaximumSize;i++)
// {
// a[i].parent = -1;
// }
// return a;
//} int main()
{ int x;
int y;
char ch;
int count;
int i = ;
scanf("%d",&MaximumSize);
tSet a[MaximumSize];
for(;i<MaximumSize;i++)
{
a[i].parent = -;
} while()
{ scanf("%c",&ch);
if(ch=='C')
{
scanf("%d %d",&x,&y);
if((FindSet(a,x)==FindSet(a,y))&&(FindSet(a,x)>=))
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
else if(ch == 'I')
{
scanf("%d %d",&x,&y);
UnionSet(a,x,y);
}
else if(ch == 'S')
{
break;
}
}
count = SetNum(a);
if(count > )
{
printf("There are %d components.\n",count);
}
else
{
printf("The network is connected.");
}
}

PAT mooc DataStructure 4-2 SetCollection的更多相关文章

  1. PAT Mooc datastructure 6-1

    Saving James Bond - Hard Version This time let us consider the situation in the movie "Live and ...

  2. PAT MOOC dataStructure 4-1

    数据结构练习 4-1 AVL 树 1. 题目: Input Specification: Each input file contains one test case. For each case, ...

  3. PAT B1080 MOOC期终成绩(C++)

    PAT甲级目录 | PAT乙级目录 题目描述 B1080 MOOC期终成绩 解题思路 可利用 map 将字符串型的学号转换为整型的序号,方便查找.输入全部成绩后,遍历每个学生同时计算最终成绩,然后将成 ...

  4. PAT 乙级 1080 MOOC期终成绩 (25 分)

    1080 MOOC期终成绩 (25 分) 对于在中国大学MOOC(http://www.icourse163.org/ )学习“数据结构”课程的学生,想要获得一张合格证书,必须首先获得不少于200分的 ...

  5. PAT 1080 MOOC期终成绩(25)(STL-map及multiset+思路+测试点分析)

    1080 MOOC期终成绩(25 分) 对于在中国大学MOOC(http://www.icourse163.org/ )学习"数据结构"课程的学生,想要获得一张合格证书,必须首先获 ...

  6. PAT 1080 MOOC期终成绩

    https://pintia.cn/problem-sets/994805260223102976/problems/994805261493977088 对于在中国大学MOOC(http://www ...

  7. PAT Basic 1080 MOOC期终成绩 (25 分)

    对于在中国大学MOOC(http://www.icourse163.org/ )学习“数据结构”课程的学生,想要获得一张合格证书,必须首先获得不少于200分的在线编程作业分,然后总评获得不少于60分( ...

  8. 【PAT】B1080 MOOC期终成绩(25 分)

    还是c++好用,三部分输入直接用相同的方法, 用map映射保存学生在结构体数组中的下标. 结构体保存学生信息,其中期末成绩直接初始化为-1, 注意四舍五入 此题还算简单 #include<ios ...

  9. PAT乙级考前总结(三)

    特殊题型 1027 打印沙漏 (20 分) 题略,感觉有点像大学里考试的题.找规律即可. #include <stdio.h>#include <iostream>using ...

随机推荐

  1. 【2016-11-3】【坚持学习】【Day18】【我认识的ORM】

    我学过或者用过的ORM有几种 EF NHibernate DevExpress  xpo (第一家公司用这个,也是很省力的orm,现在已经不记得了.) 今天晚上想找一下有没有轻量级的orm: flue ...

  2. 游戏测评-桥梁建造系Poly Bridge破力桥?游戏测评

    最近在b站看到了谜之声的视频:大家来造桥吧! 实在是太搞笑了,看到是一款新出不久还未正式发行的游戏,兴致一来便入手玩了玩.顺手也就写下了这篇测评. POLY BRIDGE 对这个游戏名怎么起个有趣的中 ...

  3. 【Python网络编程】利用Python进行TCP、UDP套接字编程

    之前实现了Java版本的TCP和UDP套接字编程的例子,于是决定结合Python的学习做一个Python版本的套接字编程实验. 流程如下: 1.一台客户机从其标准输入(键盘)读入一行字符,并通过其套接 ...

  4. sql left join on

    select sysuser.userid,       sysuser.groupid,       sysuser.sysid,       nvl( userjd.mc,nvl(useryy.m ...

  5. [No00007A]没有文件扩展".js"的脚本引擎 解决办法

    在命令行运行JScript脚本时,遇到如下的错误提示: “输入错误: 没有文件扩展“.js”的脚本引擎.” 这样的错误,原因是因为JS扩展名的文件被其他软件关联了,需要取消关联. 如系统中安装了ULT ...

  6. [No000067]Js中获取当前页面的滚动条纵坐标位置scrollTop

    三种方法任选其一: var sTop = document.body.scrollTop+document.documentElement.scrollTop; var sTop = document ...

  7. fcntl函数

    很多时候,当我们有多个进程要访问同一个文件的时候,为了防止多进程访问导致的不一致,我们就要考虑进程间的同步问题. fcntl是一个很强大的函数,我们可以通过它给文件的某一部分上锁 int fcntl( ...

  8. button 样式

    /** * 个人资料 */Ext.define('For.view.personal.MyPersonalData',{            extend:'Ext.panel.Panel',   ...

  9. jquery替换所有逗号

    代码: var aa= $("input[name=aa]").val().replace(/\,/g,""); 原文:http://blog.csdn.net ...

  10. 基于Nginx dyups模块的站点动态上下线并实现简单服务治理

    简介 今天主要讨论一下,对于分布式服务,站点如何平滑的上下线问题. 分布式服务 在分布式服务下,我们会用nginx做负载均衡, 业务站点访问某服务站点的时候, 统一走nginx, 然后nginx根据一 ...