数据结构习题集-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. vb上位机模拟电压监测系统

    vb作为一种古老的语言,在工作中已经用不到了,但这门语言也是我在校期间研究比较多的一种,基本的通讯,数据库,界面等模块已经比较了解,马上要进单位实习了,研究的是电机的变频器,软件这块,希望在以后的工作 ...

  2. 利用DotSpatial发布WMS, WFS服务

    我们遇到的几个给政府部门做的GIS系统,一般都只要面子,只要好看,领导高兴得不得了,点点这里点点那里,哟,这按钮一点还会转,领导开心得跟朵花似的...要是搞个各种分析什么的全堆上来,他就嫌烦了...这 ...

  3. ReactNative新手学习之路01-创建项目开始

    新手学习之路01-创建项目开始 小菜鸟准备学习RN开发,决定写下自己的学习历程,方便其他也想要学习RN的人,后期会持续更新写下自己所有学习经历,一步步从菜鸟成长成业内高手.开发环境准备,本文默认环境已 ...

  4. C/C++实践笔记 007

    进制输出自己写一个_itoa 进制转换void main(){ int num = 0; scanf("%d", &num); printf("num=%i&qu ...

  5. Javascript execCommon

    http://blog.csdn.net/woshinia/article/details/18664903 https://developer.mozilla.org/zh-CN/docs/Web/ ...

  6. jQuery控制表头

    <!doctype html><html lang="en"><head> <meta charset="UTF-8" ...

  7. NSArray 倒序 输出

    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"1",@"2",@"3" ...

  8. 【Codeforces715C&716E】Digit Tree 数学 + 点分治

    C. Digit Tree time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ...

  9. eclipse安装和配置

    一.下载eclipse eclipse下载页 (选择"Eclipse IDE for Java EE Developers",适用于web和android开发) 我用的是luna的 ...

  10. linux系统下sendmail的搭建

    学习鸟哥linux私房菜所得 sendmail 可以使用rpm -qa |grep sendmail来查看一下是否已安装sendmail-cf和sendmail 如果没有安装可用yum -y inst ...