pta 编程题15 列出连通集
其它pta数据结构编程题请参见:pta
题目要求分别以深度优先搜索和广度优先搜索输出图的连通集。
广度优先搜索要用到队列,先回顾一下循环队列:
struct QNode {
int* Data; /* 存储元素的数组 */
int Front, Rear; /* 队列的头、尾指针 */
int MaxSize; /* 队列最大容量 */
};
typedef struct QNode *Queue;
Queue CreateQueue( int MaxSize )
{
Queue Q = new QNode;
Q->Data = new int[MaxSize];
Q->Front = Q->Rear = ;
Q->MaxSize = MaxSize;
return Q;
}
bool IsFull( Queue Q )
{
return ((Q->Rear+)%Q->MaxSize == Q->Front);
}
void enQueue( Queue Q, ElementType X )
{
Q->Rear = (Q->Rear+)%Q->MaxSize;
Q->Data[Q->Rear] = X;
}
bool IsEmpty( Queue Q )
{
return (Q->Front == Q->Rear);
}
int deQueue( Queue Q )
{
Q->Front =(Q->Front+)%Q->MaxSize;
return Q->Data[Q->Front];
}
注意广度优先搜索BFS要在一个顶点入队的时候将其标记,而不是出队的时候。
另外c++全局变量会默认初始化。
还有形参要加上引用符号,否则改变不了实参的值。
#include <iostream>
using namespace std; struct Queue
{
int data[];
int head = ;
int tail = ;
}; int G[][]; //全局变量已
bool marked[], marked2[]; //默认初始化
void buildGraph(int E);
void dfs(int s);
void bfs(int s); void enQueue(Queue& q, int a);
int deQueue(Queue& q);
bool isEmpty(Queue q); int main()
{
int N, E, i;
cin >> N >> E;
buildGraph(E);
for (i = ; i < N; i++)
{
if (!marked[i])
{
cout << "{";
dfs(i);
cout << " }" << endl;
}
}
for (i = ; i < N; i++)
if (!marked2[i])
bfs(i);
return ;
} void buildGraph(int E)
{
int v1, v2, i;
for (i = ; i < E; i++)
{
cin >> v1 >> v2;
G[v1][v2] = ;
G[v2][v1] = ;
}
} void dfs(int s)
{
cout << " " << s;
marked[s] = true;
for (int i = ; i < ; i++)
{
if (G[s][i] && !marked[i])
dfs(i);
}
} void bfs(int s)
{
int t, i;
Queue q;
enQueue(q, s);
marked2[s] = true;
cout << "{";
while (!isEmpty(q))
{
t = deQueue(q);
cout << " " << t;
for (i = ; i < ; i++)
{
if (G[t][i] && !marked2[i])
{
enQueue(q, i);
marked2[i] = true;
}
}
}
cout << " }" << endl;
} void enQueue(Queue& q, int a)
{
q.tail = (q.tail + ) % ;
q.data[q.tail] = a;
} int deQueue(Queue& q)
{
q.head = (q.head + ) % ;
return q.data[q.head];
} bool isEmpty(Queue q)
{
return q.head == q.tail;
}
pta 编程题15 列出连通集的更多相关文章
- pta编程题19 Saving James Bond 2
其它pta数据结构编程题请参见:pta 题目 和简单版本不同的是,简单版本只需判断能否到达岸边,而这个版本要求求出最少跳数的路径. 简单版本用dfs实现,而这道题用BFS实现. 注意: 岛半径为7.5 ...
- pta 编程题20 旅游规划
其它pta数据结构编程题请参见:pta 题目 这个最短路径问题只需要求两点之间的最短路径,因而在Dijikstra算法中当求出目标点的最短路径之后跳出循环即可. #include <iostre ...
- pta 编程题21 公路村村通
其它pta数据结构编程题请参见:pta 题目 这道题考察最小生成树问题,用的是Prim算法. 和Dijkstra算法相比,没有了collect数组,因为dist[v] == 0就代表v被已收录. #i ...
- pta 编程题16 Saving James Bond - Easy Version
其它pta数据结构编程题请参见:pta 题目 主要用到了深度优先搜索. #include <iostream> using namespace std; struct Vertex { i ...
- pta 编程题14 Huffman Codes
其它pta数据结构编程题请参见:pta 题目 题目给出一组字母和每个字母的频数,因为哈夫曼编码不唯一,然后给出几组编码,因为哈夫曼编码不唯一,所以让你判断这些编码是否符合是哈夫曼编码的一种. 解题思路 ...
- pta 编程题13 File Transfer
其它pta数据结构编程题请参见:pta 这道题考察的是union-find并查集. 开始把数组中每个元素初始化为-1,代表没有父节点.为了使树更加平衡,可以让每一个连通分量的树根的负值代表这个连通分量 ...
- pta 编程题12 堆中的路径
其它pta数据结构编程题请参见:pta 这道题考察的是最小堆. 堆是一个完全二叉树,因此可用数组表示,一个下标为 i 的结点的父节点下标为 i / 2,子结点下标为 2i 和 2i + 1. 插入元素 ...
- pta 编程题10 Root of AVL Tree
其它pta数据结构编程题请参见:pta 这道题考察平衡二叉查找树的插入. 为了保证二叉查找树的平衡,当一个结点的左右子树的高度差大于1时就要进行调整. 分为以下四种情况: 插入新节点后,以及旋转之后, ...
- pta 编程题8 Tree Traversals Again
其它pta数据结构编程题请参见:pta 这次的作业考察的是树的遍历. 题目的输入通过栈的pop给出了树的中序遍历的顺序.根据push和pop的顺序构造树的方法为:定义一个变量father来确定父节点, ...
随机推荐
- c/c++/c# 快速计算 Cumulative Normal Distribution 正态累积函数CDF
链接: http://stackoverflow.com/questions/2328258/cumulative-normal-distribution-function-in-c-c http:/ ...
- 通过jQuery实现AJAX
通过jQuery实现AJAX > 使用get和getJSON都会有缓存问题,并且使用get方法不能传送较多的数据. 问题: 在IE浏览器中,get请求使用ajax存在缓存问题,会使用上一次请求的 ...
- EL表达式的语法介绍及九大隐含对象
一. 简介 > JSP表达式 <%= %> 用于向页面中输出一个对象. > 到JSP2.0时,在我们的页面中不允许出现 JSP表达式和 脚本片段. > 使用EL表达式来代 ...
- Vue中全局导入和按需导入的区别
export {router} //按需导出 import {router} from './router' //按需导入路由模块 export default //全局导出store模块 store ...
- 分别使用http,express,koa请求第三方接口
今天又再次恶补了一下http的内容,确切地说是node.js里面的http的内容,啊,百度了半天express怎么请求第三方接口,结果发现自己买的入门书籍都有这个内容.舍近求远,我真是醉了.还有百度上 ...
- Codeforces Round #365 (Div. 2) A
Description Mishka is a little polar bear. As known, little bears loves spending their free time pla ...
- 读取文件名称cmd命令
操作步骤: 1.进入命令提示符窗口 开始→运行,键入“CMD”,确定. 开始→程序→附件→C:\命令提示符 2.进入驱动器d: C:\Documents and Settings>d:(回车) ...
- spring boot之 Bean的初始化和销毁(4)
原文:https://blog.csdn.net/z3133464733/article/details/79189699 -------------------------------------- ...
- jquery——属性操作、特殊效果
1. attr().prop() 取出或者设置某个属性的值 <!DOCTYPE html> <html lang="en"> <head> &l ...
- JS语法字典---网友总结
1.document.write(""); 输出语句2.JS中的注释为//3.传统的HTML文档顺序是:document->html->(head,body)4.一个浏 ...