数据结构习题集-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. spark on yarn 提交任务出错

    Application ID is application_1481285758114_422243, trackingURL: http://***:4040Exception in thread ...

  3. Leetcode: sliding window maximum

    August 7, 2015 周日玩这个算法, 看到Javascript Array模拟Deque, 非常喜欢, 想用C#数组也模拟; 看有什么新的经历. 试了四五种方法, 花时间研究C# Sorte ...

  4. vi快捷键

    /** * eclipse内置快捷: * * * 导入包:Ctrl+Shift+O * Ctrl+T 查看一个类的继承关系树,是自顶向下的,再多按一次Ctrl+T, 会换成自底向上的显示结构. 提示: ...

  5. [zz]如何在C语言程序中处理汉字

    学习过C语言的人也许有时会遇到这样一个问题:如何用变量存储汉字以及对这些变量进行操作.目前许多C语言参考书中都没涉及到这个问题,程序中多为处理英文变量和英文字符串,涉及到汉字的情况也大都是在print ...

  6. .net session_end

    在做asp.net编程开发的时候,我遇见这样的问题,一个用户只能在一台机器上登录, 如果有用户在其他机器上登录,系统将提示该用户已经登录!当前登陆非法!我的做法是: 用Application变量保存已 ...

  7. PyCharm3.0默认快捷键(翻译的)

    PyCharm3.0默认快捷键(翻译的) PyCharm Default Keymap 1.编辑(Editing) Ctrl + Space    基本的代码完成(类.方法.属性)Ctrl + Alt ...

  8. 可运行jar包的几种打包/部署方式

    java项目开发中,最终生成的jar,大概可分为二类,一类是一些通用的工具类(不包含main入口方法),另一类是可直接运行的jar包(有main入口方法),下面主要讲的是后者,要让一个jar文件可直接 ...

  9. VS2012使用Git并连接到osc@git

    1.下载GitExtensions并安装 在http://sourceforge.net/projects/gitextensions/files/latest/download 下载 安装时请注意 ...

  10. Web前端面试题目及答案汇总

    HTML/CSS部分 1.什么是盒子模型? 在网页中,一个元素占有空间的大小由几个部分构成,其中包括元素的内容(content),元素的内边距(padding),元素的边框(border),元素的外边 ...