6-16 Topological Sort(25 分)
Write a program to find the topological order in a digraph.
Format of functions:
bool TopSort( LGraph Graph, Vertex TopOrder[] );
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 topological order is supposed to be stored in TopOrder[] where TopOrder[i] is the i-th vertex in the resulting sequence. The topological sort cannot be successful if there is a cycle in the graph -- in that case TopSort must return false; otherwise return true.
Notice that the topological order might not be unique, but the judge's input guarantees the uniqueness of the result.
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 0 to MaxVertexNum-1 */
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 TopSort( LGraph Graph, Vertex TopOrder[] );
int main()
{
int i;
Vertex TopOrder[MaxVertexNum];
LGraph G = ReadG();
if ( TopSort(G, TopOrder)==true )
for ( i=0; i<G->Nv; i++ )
printf("%d ", TopOrder[i]);
else
printf("ERROR");
printf("\n");
return 0;
}
/* Your function will be put here */
Sample Input 1 (for the graph shown in the figure):
5 7
1 0
4 3
2 1
2 0
3 2
4 1
4 2
Sample Output 1:
4 3 2 1 0
Sample Input 2 (for the graph shown in the figure):
5 8
0 3
1 0
4 3
2 1
2 0
3 2
4 1
4 2
Sample Output 2:
ERROR
bool TopSort( LGraph Graph, Vertex TopOrder[] )
{
int c = ;
int book[Graph -> Nv],h[Graph -> Nv + ],head = ,tail = ;
PtrToAdjVNode t;
for(int i = ;i < Graph -> Nv;i ++)
book[i] = ;
for(int i = ;i < Graph -> Nv;i ++)
{
t = Graph -> G[i].FirstEdge;
while(t)
{
book[t -> AdjV] ++;
t = t -> Next;
}
}
for(int i = ;i < Graph -> Nv;i ++)
{
if(book[i] == )
{
h[tail ++] = i;
}
}
if(head == tail)return false;
while(head < tail)
{
t = Graph -> G[h[head]].FirstEdge;
while(t)
{
if(book[t -> AdjV] <= )return false;
book[t -> AdjV] --;
if(book[t -> AdjV] == )h[tail ++] = t -> AdjV;
t = t -> Next;
}
book[h[head]] = -;
TopOrder[c ++] = h[head ++];
}
if(c != Graph -> Nv)return false;///有回路
return true;
}
6-16 Topological Sort(25 分)的更多相关文章
- PTA 09-排序3 Insertion or Heap Sort (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/676 5-14 Insertion or Heap Sort (25分) Accor ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
- 【PAT甲级】1101 Quick Sort (25 分)
题意: 输入一个正整数N(<=1e5),接着输入一行N个各不相同的正整数.输出可以作为快速排序枢纽点的个数并升序输出这些点的值. trick: 测试点2格式错误原因:当答案为0时,需要换行两次
- A1101 Quick Sort (25 分)
一.技术总结 这里的一个关键就是理解调换位置排序是时,如果是元主,那么它要确保的条件就只有两个一个是,自己的位置不变,还有就是前面的元素不能有比自己大的. 二.参考代码 #include<ios ...
- 09-排序3 Insertion or Heap Sort (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 【PAT甲级】1098 Insertion or Heap Sort (25 分)
题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...
- 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 ...
- 1098 Insertion or Heap Sort (25分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- PTA 10-排序6 Sort with Swap(0, i) (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/678 5-16 Sort with Swap(0, i) (25分) Given a ...
随机推荐
- JavaScript Ajax上传文件miniupload.js
用到jquery和layer.js (function ($) { $.fn.miniupload = function (options, callback) { var jqDom = $(thi ...
- capistranorb
远程服务器自动部署工具 https://capistranorb.com/
- bzoj1627 / P2873 [USACO07DEC]泥水坑Mud Puddles
P2873 [USACO07DEC]泥水坑Mud Puddles bfs入门. 对于坐标为负的情况,我们可以给数组下标加上$abs(min(minx,miny))$转正(根据题意判断) #includ ...
- vue中实现中,自动补全功能
知识点:利用vue的基本语法实现,自动补全功能 参考博客:https://www.jb51.net/article/136282.htm 效果:在文本框中,输入相关名称,调用后台接口,将数据填充到下拉 ...
- 初始化 Flask 虚拟环境 命令
参考:<Flask Web开发> 系统:Mac OSX CMD // 激活环境 virtualenv venv source venv/bin/activate // 安装flask pi ...
- Mongodb 命令及 PyMongo 库的使用
1. PyMongo import pymongo 1. 初始化 Mongo 客户端 client=pymongo.MongoClient(mongodb://10.85.39.45:8188,10. ...
- UVa 1664 Conquer a New Region(并查集)
https://vjudge.net/problem/UVA-1664 题意: n个城市形成一棵树,每条边有权值C(i,j).任意两个点的容量S(i,j)定义为i与j唯一通路上容量的最小值.找一个点, ...
- mysql更改数据文件目录及my.ini位置
步骤: 1.查找my.ini位置,可通过windows服务所对应mysql启动项,查看其对应属性->可执行文件路径,获取my.ini路径. "C:\MySQL\MySQL Server ...
- vue中watch的用法
一.首先确认watch是一个对象,一定要当做对象来用 watch:{ } 对象:有键,有值. 1.键:就是你要监控的那个家伙,比如说$route,这个就是要监控路由的变化.或者是data中的某个变量. ...
- MaintainableCSS 《可维护性 CSS》 --- 模板篇
什么是模块(Modules) ? 模块是一个特别的独立单元,可以与其他模块组合以形成更复杂的结构. 在客厅里,我们可以认为电视,沙发和墙艺术是模块.它们聚在一起创造一个可用的房间. 如果我们把其中一个 ...