06-图1 列出连通集 (25分)(C语言邻接表实现)
题目地址:https://pta.patest.cn/pta/test/558/exam/4/question/9495
由于边数E<(n*(n-1))/2 所以我选用了邻接表实现,优先队列用循环队列实现;
DFS基本思路:
1:选择一个点,标志已经访问过;
2:判断这个点的其他邻接点(访问顺序按题目是从小到大)是否访问过,来选择下一个点;
3:重复第2点直到全部点已经访问过。
伪代码如下
DFS( Vertex v )
{
Visit( V );
Visited[V] = true;
foreach( v->neighbor )
if ( Visited[v->neighbor ] == false )
DFS(v->neighbor );
}
BFS基本思路:
1:选择一个点入队,并访问这个点,标志已经访问过;
2:出队,将这个点未访问过的全部邻点访问并入队;
3:重复第二点直到队列为空;
伪代码如下
BFS ( Vertex v )
{
Visit( v );
Visited[v] = true;
EnQueue(Q, v);
while ( !IsEmpty(Q) )
{
w = DeQueue(Q);
foreach ( w->neighor )
if ( Visited[w->neighor] == false )
{
Visit( w->neighor );
Visited[w->neighor] = true;
EnQueue(Q, w->neighor);
}
}
}
具体代码
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <dos.h>
#define MaxVertexNum 10
typedef int ElementType;
typedef int Position;
typedef struct QNode {
ElementType *Data; /* 存储元素的数组 */
Position Front, Rear; /* 队列的头、尾指针 */
int MaxSize; /* 队列最大容量 */
}QNode, *Queue;
typedef struct ENode
{
int ivex; //该边指向的顶点位置
struct ENode * next;
}ENode, *PENode;
typedef int VertexType;
typedef struct VNode
{
VertexType data;
ENode * first_edge;
}VNode;
typedef struct LGraph
{
int vexnum; //图的顶点数目
int edgnum; //图的边的数目
VNode * vexs; //存放顶点的数组
}LGraph;
bool TAG; //用于输出格式
bool visited[MaxVertexNum];
LGraph * LGraph_new();
void LGraph_destroy(LGraph * G);
void LGraph_insert(ENode ** head, int ivex);
void ResetVisit();
void DFS(LGraph * G, int vex);
void BFS(LGraph * G, int vex);
void ListComponents(LGraph * G, void (*func)(LGraph * G, int vex));
//优先队列的基本操作
Queue CreateQueue( int MaxSize );
bool IsFull( Queue Q );
bool AddQ( Queue Q, ElementType X );
bool IsEmpty( Queue Q );
ElementType DeleteQ( Queue Q );
int main()
{
LGraph * G = LGraph_new();
ResetVisit();
ListComponents(G, DFS);
ResetVisit();
ListComponents(G, BFS);
system("pause");
;
}
void ListComponents(LGraph * G, void (*func)(LGraph * G, int vex))
{
int i;
; i < G->vexnum; ++i )
{
if (visited[i] == false)
{
(*func)(G, i);
printf("}\n");
TAG = false;
}
}
}
LGraph * LGraph_new()
{
LGraph * G = (LGraph *)malloc(sizeof(LGraph));
scanf("%d %d", &G->vexnum, &G->edgnum);
G->vexs = (VNode *)malloc(G->vexnum * sizeof(VNode));
int i, v1, v2;
; i < G->vexnum; ++i )
{
G->vexs[i].data = i;
G->vexs[i].first_edge = NULL;
}
; i < G->edgnum; ++i )
{
scanf("%d %d", &v1, &v2);
//由于顶点信息就是顶点坐标
LGraph_insert(&G->vexs[v1].first_edge, v2);
LGraph_insert(&G->vexs[v2].first_edge, v1);
}
return G;
}
void ResetVisit()
{
TAG = false;
memset(visited, false, sizeof(visited));
}
void LGraph_insert(ENode ** head, int ivex)
{
ENode *pnew, *p1, *p2;
pnew = (ENode *)malloc(sizeof(ENode));
pnew->ivex = ivex;
if ( *head == NULL )
{
*head = pnew;
pnew->next = NULL;
}
else
{
if ( (*head)->ivex > ivex ) //没头结点的麻烦
{
pnew->next = *head;
*head = pnew;
}
else
{
for (p1 = *head, p2 = p1->next; p2 != NULL ; p1 = p2, p2 = p1->next )
{
if ( p2->ivex > ivex )
break;
}
pnew->next = p2;
p1->next = pnew;
}
}
}
void DFS(LGraph * G, int vex)
{
if (TAG == false)
{
TAG = true;
printf("{ ");
}
printf("%d ", vex);
visited[vex] = true;
ENode * temp = G->vexs[vex].first_edge;
while( temp != NULL )
{
if (visited[temp->ivex] == false)
{
DFS(G, temp->ivex);
}
temp = temp->next;
}
}
void BFS(LGraph * G, int vex)
{
Queue Q = CreateQueue(G->vexnum);
if (TAG == false)
{
TAG = true;
printf("{ ");
}
printf("%d ", vex);
visited[vex] = true;
AddQ(Q, vex);
while (!IsEmpty(Q))
{
vex = DeleteQ(Q);
ENode * temp = G->vexs[vex].first_edge;
while (temp != NULL)
{
if ( visited[temp->ivex] == false )
{
printf("%d ",temp->ivex);
visited[temp->ivex] = true;
AddQ(Q, temp->ivex);
}
temp = temp->next;
}
}
free(Q->Data);
free(Q);
}
//队列基本操作
Queue CreateQueue( int MaxSize )
{
Queue Q = (Queue)malloc(sizeof(struct QNode));
Q->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
Q->Front = Q->Rear = ;
Q->MaxSize = MaxSize;
return Q;
}
bool IsFull( Queue Q )
{
)%Q->MaxSize == Q->Front);
}
bool AddQ( Queue Q, ElementType X )
{
if ( IsFull(Q) ) {
printf("队列满");
return false;
}
else {
Q->Rear = (Q->Rear+)%Q->MaxSize;
Q->Data[Q->Rear] = X;
return true;
}
}
bool IsEmpty( Queue Q )
{
return (Q->Front == Q->Rear);
}
ElementType DeleteQ( Queue Q )
{
if ( IsEmpty(Q) ) {
printf("队列空");
;
}
else {
Q->Front =(Q->Front+)%Q->MaxSize;
return Q->Data[Q->Front];
}
}
06-图1 列出连通集 (25分)(C语言邻接表实现)的更多相关文章
- PTA - - 06-图1 列出连通集 (25分)
06-图1 列出连通集 (25分) 给定一个有NN个顶点和EE条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N-1N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发, ...
- 图论——图的邻接表实现——Java语言(完整demo)
1.图的简单实现方法——邻接矩阵 表示图的一种简单的方法是使用一个一维数组和一个二维数组,称为领接矩阵(adjacent matrix)表示法. 对于每条边(u,v),置A[u,v]等于true:否则 ...
- 【(图) 旅游规划 (25 分)】【Dijkstra算法】
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> us ...
- 1090 危险品装箱 (25分)C语言
集装箱运输货物时,我们必须特别小心,不能把不相容的货物装在一只箱子里.比如氧化剂绝对不能跟易燃液体同箱,否则很容易造成爆炸. 本题给定一张不相容物品的清单,需要你检查每一张集装箱货品清单,判断它们是否 ...
- 1050 螺旋矩阵 (25 分)C语言
本题要求将给定的 N 个正整数按非递增的顺序,填入"螺旋矩阵".所谓"螺旋矩阵",是指从左上角第 1 个格子开始,按顺时针螺旋方向填充.要求矩阵的规模为 m 行 ...
- 1065 单身狗 (25分)C语言
单身狗"是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数 N(≤ 50 000),是已知夫妻/伴侣的对数:随 ...
- 1060 爱丁顿数 (25 分)C语言
英国天文学家爱丁顿很喜欢骑车.据说他为了炫耀自己的骑车功力,还定义了一个"爱丁顿数" E ,即满足有 E 天骑车超过 E 英里的最大整数 E.据说爱丁顿自己的 E 等于87. 现给 ...
- 1025 反转链表 (25 分)C语言
题目描述 给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转.例如:给定L为1→2→3→4→5→6,K为3,则输出应该为 3→2→1→6→5→4:如果K为4,则输出应该为4→3→2→1→5 ...
- 1055 集体照 (25 分)C语言
拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每排人数为 N/K(向下取整),多出来的人全部站在最后一排: 后排所有人的个子都不比前排任何人矮: 每排中最高者站中间(中 ...
随机推荐
- 《Entity Framework 6 Recipes》中文翻译系列 (9) -----第二章 实体数据建模基础之继承关系映射TPH
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 2-10 Table per Hierarchy Inheritance 建模 问题 ...
- VB6.0中,DTPicker日期、时间控件不允许为空时,采用文本框与日期、时间控件相互替换赋值(解决方案)
VB6.0中,日期.时间控件不允许为空时,采用文本框与日期.时间控件相互替换赋值,或许是一个不错的选择. 实现效果如下图: 文本框txtStopTime1 时间框DTStopTime1(DTPicke ...
- C#设计模式-建造者模式
在软件系统中,有时需要创建一个复杂对象,并且这个复杂对象由其各部分子对象通过一定的步骤组合而成. 例如一个采购系统中,如果需要采购员去采购一批电脑时,在这个实际需求中,电脑就是一个复杂的对象,它是由C ...
- JS数组定义及详解
1.什么是数组 数组就是一组数据的集合 其表现形式就是内存中的一段连续的内存地址 数组名称其实就是连续内存地址的首地址 2.关于js中的数组特点 数组定义时无需指定数据类型 数组定义时可以无需指定数组 ...
- sun.misc.BASE64Encoder找不到jar包的解决方法
1.右键项目->属性->java bulid path->jre System Library->access rules->resolution选择accessible ...
- Security2:Create User
User 用于访问DB Users based on logins in master (This is the most common type of user.) User based on a ...
- SQL Server 解读【已分区索引的特殊指导原则】(1)- 索引对齐
一.前言 在MSDN上看到一篇关于SQL Server 表分区的文档:已分区索引的特殊指导原则,如果你对表分区没有实战经验的话是比较难理解文档里面描述的意思.这里我就里面的一些概念进行讲解,方便大家的 ...
- Create views of OpenCASCADE objects in the Debugger
Create views of OpenCASCADE objects in the Debugger eryar@163.com Abstract. The Visual Studio Natvis ...
- 窥探Swift编程之强大的Switch
之前初识Swift中的Switch语句时,真的是让人眼前一亮,Swift中Switch语句有好多特有而且特好用的功能.说到Switch, 只要是写过程序的小伙伴对Switch并不陌生.其在程序中的出镜 ...
- Kubernetes集群搭建过程中遇到的问题
1. 创建Nginx Pod过程中报如下错误: #kubectlcreate -f nginx-pod.yaml Error from server: error when creating &quo ...