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. 全国疫情精准定点动态更新(.net core)

    前言 疫情远比我们在年初想的发展迅速,在过年前还计划着可以亲戚聚聚,结果都泡汤了,开始了自家游. 在初三的时候,看到那个丁香医生,觉得不够详细,比如说我想看下周边城市的疫情情况,但是我地理不好,根本不 ...

  2. Java集合XMind与注意事项

    Java中集合使用时的几个注意事项: 1.ArrayList和HashMap都具有扩容 ArrayList初始化数组长度为10,扩容后的容量为原来的1.5倍. HashMap初始化的数组长度为16,扩 ...

  3. [兴趣使然]用python在命令行下画jandan像素超载鸡

    下午刷煎蛋的时候看到 Dthalo 蛋友发的系列像素超载鸡,就想自己试试用python脚本画一个,老男孩视频里的作业真没兴趣,弄不好吧没意思,往好了写,自己控制不好,能力不够. 所以还是找自己有兴趣的 ...

  4. Codeforces_334_C

    http://codeforces.com/problemset/problem/334/C 求不能凑整n,和最小,数量最多的数量. #include<iostream> #include ...

  5. Codeforces 924 A Tritonic Iridescence(暴力集合交集、相等)

    题目链接:点击打开链接 There is a rectangular grid of n rows of m initially-white cells each. Arkady performed ...

  6. 在家想自学Java,有C语言底子,请问哪本书适合?

    一.问题剖析 看到这个问题,我想吹水两句再做推荐.一般发出这个疑问都处在初学编程阶段,编程语言都是相通的,只要你领悟了一门语言的"任督二脉",以后你学哪一门语言都会轻易上手.学语言 ...

  7. Jmeter之上传文件

    前言 我们可以利用postman工具来测试上传文件的接口,那么假如要利用Jmeter工具来进行上传接口的测试,又该如何测试呢? 上传文件的接口地址:/pinter/file/api/upload:接口 ...

  8. js发展历史与基础

    最早的浏览器是WWW浏览器 Mosaic浏览器是互联网历史上第一个获普遍使用和能够显示图片的网页浏览器,于1993年问世 浏览器组成部分: 1)shell部分(外壳) 2)内核部分 ① 渲染引擎(语法 ...

  9. 03(a)多元有约束优化问题(准备知识)

    转成Latex上传太麻烦,直接截图上传了,需要电子版的可以关注一下,微信公众号:“实干小海豹”,回复:”优化01a“,”优化01b“,”优化02a“,”优化02b“,”优化02c“,”优化02c“.. ...

  10. Html / XHtml 解析 - Parsing Html and XHtml

    Html / XHtml 解析 - Parsing Html and XHtml HTMLParser 模块 通过 HTMLParser 模块来解析 html 文件通常的做法是, 建立一个 HTMLP ...