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. 图:无向图(Graph)基本方法及Dijkstra算法的实现 [Python]

    一般来讲,实现图的过程中需要有两个自定义的类进行支撑:顶点(Vertex)类,和图(Graph)类.按照这一架构,Vertex类至少需要包含名称(或者某个代号.数据)和邻接顶点两个参数,前者作为顶点的 ...

  2. java操作Word总结

    import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Varia ...

  3. ubuntu-未信任的应用程序启动器-XX-Net.desktop

      在安装启动xxnet时使用sudo命令,该软件打开后提示[未信任的应用程序启动器]如图所示,解决办法简介:(1)更换成root用户(2)更改权限   背景描述 xx-net中的启动程序有权限设置, ...

  4. open-falcon api相关

    本文描述通过被监控endpoint的名称获取该endpoint的eid和监控项,从而获取到该endpoint的监控历史数据,使用python代码的 api操作方法 注:同步open-falcon和ag ...

  5. linux及安全第七周总结——20135227黄晓妍

    实验部分 首先clone最新的menu 我们可以看到,test.c里多了一个exec的功能,它的代码和fork基本一致,多了一项加载hello rootfs也有一些变化 执行一下exec 让我们启动一 ...

  6. JS实现焦点图轮播效果

    大家平时逛淘宝网的时候,在首页就能看到焦点图轮播的效果,就是这个样子的: PS:想起每每打开淘宝,总会被这个玩意先夺眼球,偶尔还去点进去溜溜,幸好我定力好,总能控制住自己的购买欲望,为自己不用剁手感到 ...

  7. 云主机搭建Kubernetes 1.10集群

    一.基础环境 云主机 下载软件包 将所有软件下载至/data目录 # 链接:https://pan.baidu.com/s/13DlR1akNBCjib5VFaIjGTQ 密码:1l69 # 链接:h ...

  8. luogu P1162 填涂颜色

    https://www.luogu.org/problem/show?pid=1162 //其实很简单的吧 //就是最外圈加一圈0 ,然后把外圈里面的0都遍历了 //剩下的0 就变成2 就行了 #in ...

  9. ZOJ 1276 Optimal Array Multiplication Sequence(矩阵连乘)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1276 裸的矩阵连乘问题. #include<iostream> ...

  10. Java网络编程学习A轮_06_NIO入门

    参考资料: 老外写的教程,很适合入门:http://tutorials.jenkov.com/java-nio/index.html 上面教程的译文:http://ifeve.com/overview ...