PAT mooc DataStructure 4-2 SetCollection
数据结构习题集-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的更多相关文章
- PAT Mooc datastructure 6-1
Saving James Bond - Hard Version This time let us consider the situation in the movie "Live and ...
- PAT MOOC dataStructure 4-1
数据结构练习 4-1 AVL 树 1. 题目: Input Specification: Each input file contains one test case. For each case, ...
- PAT B1080 MOOC期终成绩(C++)
PAT甲级目录 | PAT乙级目录 题目描述 B1080 MOOC期终成绩 解题思路 可利用 map 将字符串型的学号转换为整型的序号,方便查找.输入全部成绩后,遍历每个学生同时计算最终成绩,然后将成 ...
- PAT 乙级 1080 MOOC期终成绩 (25 分)
1080 MOOC期终成绩 (25 分) 对于在中国大学MOOC(http://www.icourse163.org/ )学习“数据结构”课程的学生,想要获得一张合格证书,必须首先获得不少于200分的 ...
- PAT 1080 MOOC期终成绩(25)(STL-map及multiset+思路+测试点分析)
1080 MOOC期终成绩(25 分) 对于在中国大学MOOC(http://www.icourse163.org/ )学习"数据结构"课程的学生,想要获得一张合格证书,必须首先获 ...
- PAT 1080 MOOC期终成绩
https://pintia.cn/problem-sets/994805260223102976/problems/994805261493977088 对于在中国大学MOOC(http://www ...
- PAT Basic 1080 MOOC期终成绩 (25 分)
对于在中国大学MOOC(http://www.icourse163.org/ )学习“数据结构”课程的学生,想要获得一张合格证书,必须首先获得不少于200分的在线编程作业分,然后总评获得不少于60分( ...
- 【PAT】B1080 MOOC期终成绩(25 分)
还是c++好用,三部分输入直接用相同的方法, 用map映射保存学生在结构体数组中的下标. 结构体保存学生信息,其中期末成绩直接初始化为-1, 注意四舍五入 此题还算简单 #include<ios ...
- PAT乙级考前总结(三)
特殊题型 1027 打印沙漏 (20 分) 题略,感觉有点像大学里考试的题.找规律即可. #include <stdio.h>#include <iostream>using ...
随机推荐
- using 释放资源
我们知道使用C#程序访问数据库资源需要几个步骤:创建连接,打开连接,访问数据库,关闭连接,基本架构如下: SqlConnection conn = new SqlConnection(connStri ...
- 洛谷P3388 【模板】割点
给出一个n个点,m条边的无向图,求图的割点. u是cut vertex的两个条件: 1.存在v使v及其所有后代没有反向边连回u的祖先 2.u是根且有两个以上子节点 dfs一遍 low[u]是u及其后代 ...
- 微信小程序之页面路由(九)
[未经允许,请勿以任何形式转载] 什么是路由? 我们通常理解的路由指分组数据包从源到目的地时,决定端到端路径的网络范围的进程: 借用上面的定义,我们可以理解小程序页面路由,根据路由规则(路径)从一个页 ...
- .Net配置中心-Zookeper版
简介 zookeeper的基本概念和作用这里不做介绍,现在很多的公司都在使用它,说起它的作用,可能最先想到的是配置中心,可以将配置项作为一个node存储在zookeeper中,其他应用可以“关注 ...
- Django的views中的request
Django使用request和response对象在系统间传递状态. 当一个页面被请示时,Django创建一个包含请求元数据的 HttpRequest 对象. 然后Django调入合适的视图,把Ht ...
- ssh中org.springframework.orm.hibernate4.support.OpenSessionInViewFilter的作用及配置
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter 是Spring为我们解决Hibernate的Session的关闭 ...
- MFC的多线程操作
记得用MFC做了一个图像自动修复软件,当时没有多线程操作这一概念,由于图像修复算法比较复杂,因此,当执行图像修复时,程序就像卡死了似得而不能做其他操作.其实MFC对这种情况有一种很好地解决方案,那就是 ...
- PHPExcel读取excel文件
<?php set_time_limit(0); $dir = dirname(__FILE__);//当前脚本所在路径 require $dir."/PHPExcel_1.8.0/C ...
- [转]win 10 开始菜单(Win 7风格)增强工具 StartIsBack++ v1.3.4 简体中文特别版
Windows10开始菜单增强工具StartIsBack++现已更新至v1.3.4,最近主要修复在Win10周年更新版上恢复睡眠后任务栏通知中心按钮消失的问题.升级版对StartIsBack+全新构建 ...
- 菜鸟初识python request属性及方法说明
if request.REQUEST.has_key('键值'): HttpRequest对象的属性 参考: 表 H-1. HttpRequest对象的属性 属性 描述 path 表示提交请求页面完 ...