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 分)的更多相关文章

  1. 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 ...

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

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

  3. 【PAT甲级】1101 Quick Sort (25 分)

    题意: 输入一个正整数N(<=1e5),接着输入一行N个各不相同的正整数.输出可以作为快速排序枢纽点的个数并升序输出这些点的值. trick: 测试点2格式错误原因:当答案为0时,需要换行两次

  4. A1101 Quick Sort (25 分)

    一.技术总结 这里的一个关键就是理解调换位置排序是时,如果是元主,那么它要确保的条件就只有两个一个是,自己的位置不变,还有就是前面的元素不能有比自己大的. 二.参考代码 #include<ios ...

  5. 09-排序3 Insertion or Heap Sort (25 分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  6. 【PAT甲级】1098 Insertion or Heap Sort (25 分)

    题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...

  7. 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 ...

  8. 1098 Insertion or Heap Sort (25分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  9. 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 ...

随机推荐

  1. uva1423 巧用拓扑排序

    对于一个序列 a1 a2 ... an 我们可以计算出一个符号矩阵A, 其中Si,j 为 a1+...+aj 的正负号,(连加和大于0则Sij=+ 小于0 Sij=-  等于0 则Sij=0), 根据 ...

  2. jmeter -xml日志格式中网络时间与服务器时间的区分

    在 LR 中是有一个“网页细分图”的,通过这个图,你可以比较容易的区分哪些请求的响应时间最长,如果响应时间过程,是消耗在server处理的时候,还是消耗在网络传输过程中——也就是所谓的 Server ...

  3. 服务器负载、CPU性能判断

    说在前面: 在linux操作系统中,我们一般查看系统的cpu负载情况常用的命令可以是uptime,top,还有vmstat等这些个都是可以有的.每个工具所提供的信息各不相同, 我这里要讨论的仅说cpu ...

  4. Cup fungus in Corvobado Nation Park,Costa Rica

  5. centos配置用户级别的jdk的环境变量

    前面讲解了centos配置jdk的环境变量 的root级别的jdk配置 ,这里讲解用户级别的jdk配置. 在用户的当前目录下,如下,有四个隐藏的文件,文件打头是.bash******: 1.编辑.ba ...

  6. C#反射——模仿BeanUtil属性复制

    反射工具类请参见:https://www.cnblogs.com/threadj/p/10535796.html using System; using System.Collections.Gene ...

  7. 利用python统计代码行

    参加光荣之路测试开发班已三月有余,吴总上课也总问“ 咱们的课上了这么多次了大家实践了多少行代码了?”.这里是一个一脸懵逼的表情.该怎么统计呢?一个个文件数当然不可取,能用代码解决的事咱们坚决不动手.最 ...

  8. CodeForces - 55D Beautiful numbers(数位DP+Hash)题解

    题意:美丽数定义:一个正数能被所有位数整除.求给出一个范围,回答这个范围内的美丽数. 思路:一个数能被所有位数整除,换句话说就是一个数能整除所有位数的LCM,所以问题就转化为一个数能否被所有位数的LC ...

  9. redis的过期策略以及内存淘汰机制

    redis采用的是定期删除+惰性删除策略. 为什么不用定时删除策略? 定时删除,用一个定时器来负责监视key,过期则自动删除.虽然内存及时释放,但是十分消耗CPU资源.在大并发请求下,CPU要将时间应 ...

  10. 《F4+2》——团队项目的原型设计与开发

      目 录 1 · 团队信息 2 · NABCD模型 3.  原型设计的工具 4 · 原型设计 5 · PSP表格 6 · 团队设计过程 7 · 原型设计心得   一 · 团 队 信 息 成 员 列 ...