Write a program to test if a give sequence Seq is a topological order of a given graph Graph.

Format of functions:

bool IsTopSeq( LGraph Graph, Vertex Seq[] );

where LGraph is defined as the following:

typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode{
Vertex AdjV;
PtrToAdjVNode Next;
}; typedef struct Vnode{
PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum]; typedef struct GNode *PtrToGNode;
struct GNode{
int Nv;
int Ne;
AdjList G;
};
typedef PtrToGNode LGraph;

The function IsTopSeq must return true if Seq does correspond to a topological order; otherwise return false.

Note: Although the vertices are numbered from 1 to MaxVertexNum, they are indexed from 0 in the LGraph structure.

Sample program of judge:

#include <stdio.h>
#include <stdlib.h> typedef enum {false, true} bool;
#define MaxVertexNum 10 /* maximum number of vertices */
typedef int Vertex; /* vertices are numbered from 1 to MaxVertexNum */ typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode{
Vertex AdjV;
PtrToAdjVNode Next;
}; typedef struct Vnode{
PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum]; typedef struct GNode *PtrToGNode;
struct GNode{
int Nv;
int Ne;
AdjList G;
};
typedef PtrToGNode LGraph; LGraph ReadG(); /* details omitted */ bool IsTopSeq( LGraph Graph, Vertex Seq[] ); int main()
{
int i, j, N;
Vertex Seq[MaxVertexNum];
LGraph G = ReadG();
scanf("%d", &N);
for (i=; i<N; i++) {
for (j=; j<G->Nv; j++)
scanf("%d", &Seq[j]);
if ( IsTopSeq(G, Seq)==true ) printf("yes\n");
else printf("no\n");
} return ;
} /* Your function will be put here */

Sample Input (for the graph shown in the figure):


Sample Output:

yes
yes
yes
no
no

题目的大致意思就是,给你一组数据,根据这组数据构建一个有向图,再给你几组序列,判断是不是拓扑序列。

思路:先确定每个结点的入度数,按拓扑顺序输出结点时,每输出一个结点,将其子结点的入度数 -1.

注意:输入的顶点是从 0 开始存放的,也就是

0 1 2 3 4
G1 G2 G3 G4 G5
struct AdjVNode{
Vertex AdjV;
PtrToAdjVNode Next;
};
中的 AdjV,也是从 0 开始存放。

代码

bool IsTopSeq( LGraph Graph, Vertex Seq[] ){
int inDegree[];
for(int i=;i<=Graph->Nv;i++)
inDegree[i]=;
PtrToAdjVNode temnode;
for(int i=;i<Graph->Nv;i++){
temnode=Graph->G[i].FirstEdge;
while (temnode){
inDegree[temnode->AdjV]++;
temnode=temnode->Next;
}
}
for(int i=;i<Graph->Nv;i++){
if(inDegree[Seq[i]-]!=)
return false;
else{
temnode=Graph->G[Seq[i]-].FirstEdge;
while(temnode){
inDegree[temnode->AdjV]--;
temnode=temnode->Next;
}
}
}
return true;
}

PTA Is Topological Order的更多相关文章

  1. A1146. Topological Order

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  2. PAT A1146 Topological Order (25 分)——拓扑排序,入度

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  3. PAT 甲级 1146 Topological Order

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343043829760 This is a problem give ...

  4. PAT 1146 Topological Order[难]

    1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which o ...

  5. [PAT] 1146 Topological Order(25 分)

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  6. PAT 1146 Topological Order

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  7. PAT_A1146#Topological Order

    Source: PAT A1146 Topological Order (25 分) Description: This is a problem given in the Graduate Entr ...

  8. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

  9. 1146. Topological Order (25)

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

随机推荐

  1. 暑假第四周总结(HDFS编程实践,安装HBASE)

    本周根据书上以及教程的提示,对HDFS进行了编程实践,将教程所给的代码(判断文件是否存在,创建文件,读取文件)进行了应用,根据视频的讲解,对一些简单的语句有了一定的了解,但还是比较生疏.另外还根据提示 ...

  2. Dubbo(三):深入理解Dubbo源码之如何将服务发布到注册中心

    一.前言 前面有说到Dubbo的服务发现机制,也就是SPI,那既然Dubbo内部实现了更加强大的服务发现机制,现在我们就来一起看看Dubbo在发现服务后需要做什么才能将服务注册到注册中心中. 二.Du ...

  3. Ops: 高效组合命令集合

    简介 本篇博客收集一些常用的复杂命令组合,这些命令组合能够高效的定位.分析.处理一些问题,希望对需要的小伙伴有所帮助. 命令集合 批量备份文件 将名称为config.xml的文件查找出来,并在原目录备 ...

  4. C++解析Json,使用JsonCpp读写Json数据

    JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式.通常用于数据交换或存储. JsonCpp是一个基于C++语言的开源库,用于C++程序的J ...

  5. 必知必会之Lambda表达式

    Java是一门强大的面向对象的语言,除了8种基本的数据类型,其他一切皆为对象.因此,在Java中定义函数或方法都离不开对象,也就意味着很难直接将方法或函数像参数一样传递,而Java8中的Lambda表 ...

  6. Ambari HDP 下 SPARK2 与 Phoenix 整合

    1.环境说明 操作系统 CentOS Linux release 7.4.1708 (Core) Ambari 2.6.x HDP 2.6.3.0 Spark 2.x Phoenix 4.10.0-H ...

  7. 各类JWT库(java)的使用与评价

    [搬运工] 出处:http://andaily.com/blog/?p=956 在 https://jwt.io/ 网站中收录有各类语言的JWT库实现(有关JWT详细介绍请访问 https://jwt ...

  8. pytorch --Rnn语言模型(LSTM,BiLSTM) -- 《Recurrent neural network based language model》

    论文通过实现RNN来完成了文本分类. 论文地址:88888888 模型结构图: 原理自行参考论文,code and comment: # -*- coding: utf-8 -*- # @time : ...

  9. Shell脚本 小程序演示

    一般的shell编程 场景贯穿了几个熟知的步骤: ●显示消息●获取用户输入●存储值到文件●处理存储的数据 这里写一个小程序 包含以上几部 #!/bin/bash while true do #echo ...

  10. [jQuery]顶级对象$(二)

    $ 是 jQuery 的缩写 <script> # 方法1. $ 是jQuery的别称 弹出提示 $(function () { alert(11) ); # 方法2 jQuery(fun ...