数据结构习题集-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. AC日记——校门外的树 洛谷 P1047

    题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,……,L,都种 ...

  2. Spring中的JDK动态代理

    Spring中的JDK动态代理 在JDK1.3以后提供了动态代理的技术,允许开发者在运行期创建接口的代理实例.在Sun刚推出动态代理时,还很难想象它有多大的实际用途,现在动态代理是实现AOP的绝好底层 ...

  3. Iterator接口。集合输出

    在集合中支持以下几种方式. iterator ListIterator foreach输出 emumeration输出. 集合输出的标准操作: 集合输出的时候必须形成以下的思路:只要碰到了集合输出的操 ...

  4. Centos6安装Gitlab

    安装参考 https://about.gitlab.com/downloads/ 可以从清华的镜像下载安装包, 注意区分自己用的是哪个发行版 https://mirror.tuna.tsinghua. ...

  5. git提交时支持文件名大小写的修改

    在windows环境下,git提交文件时,默认对文件名大小写不敏感,若修改了文件名字的大小写,可能会导致提交时没有记录,文件名修改不成功.网上搜集了几种解决方法,现总结下: 1. 修改git conf ...

  6. Dell xps 13 9350待机时总是关机的处理方法

    现象: 年初买的dell xps13 9350,最近可能是由于win10做了一些更新,每次睡眠就自动关机了,重启很多次,修改电源选项都没用, 原因分析: 在网上搜了一下,有人发现xps15 9350也 ...

  7. QString, string, int, char* 之间相互转换

    这三种数据类型在实际运用中经常需要互相转换,那么这里小结下它们之间的转换方法: - Qstring & string Qt中封装的类十分强大,其成员函数数量之多比STD有过之而无不及,许多程序 ...

  8. Golang gzip的压缩和解压

    package src import ( "bytes" "compress/gzip" ) func GzipEncode(in []byte) ([]byt ...

  9. 2.1 python使用MongoDB 示例代码

    import pymongo client = pymongo.MongoClient('localhost', 27017) # MongoDB 客户端 walden = client['walde ...

  10. MySQL连表操作之一对多

    引入 当我们在数据库中创建表的时候,有可能某些列中值内容量很大,而且重复. 例子:创建一个学生表,按学校年纪班级分,表的内容大致如下: id name partment 1 xxx x学校x年级x班级 ...