PTA Is Topological Order
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的更多相关文章
- A1146. Topological Order
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...
- 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 ...
- PAT 甲级 1146 Topological Order
https://pintia.cn/problem-sets/994805342720868352/problems/994805343043829760 This is a problem give ...
- PAT 1146 Topological Order[难]
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which o ...
- [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 ...
- PAT 1146 Topological Order
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...
- PAT_A1146#Topological Order
Source: PAT A1146 Topological Order (25 分) Description: This is a problem given in the Graduate Entr ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
- 1146. Topological Order (25)
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...
随机推荐
- Oracle 11g 单实例静默安装实战记录(linux)
oracle 11g 单实例静默安装 AUTHOR:Oracle_Ran 环境规划: OS Version : Red Hat Enterprise Linux Server release 6.7 ...
- Codeforces_711_B
http://codeforces.com/problemset/problem/711/B 比较简单,过程有点繁琐,先找一行包含那个0的行,得到和,以此填出0位置的值,然后判断这个矩阵是否符合条件. ...
- LeetCode22 生成所有括号对
本文始发于个人公众号:TechFlow,原创不易,求个关注 链接 Generate Parentheses 难度 Medium 描述 Given n pairs of parentheses, wri ...
- Asp.Net Core IdentityServer4 管理面板集成
前言 IdentityServer4(以下简称 Id4) 是 Asp.Net Core 中一个非常流行的 OpenId Connect 和 OAuth 2.0 框架,可以轻松集成到 Asp.Net C ...
- Jdk14 都要出了,Jdk9 的新特性还不了解一下?
Java 9 中最大的亮点是 Java 平台模块化的引入,以及模块化 JDK.但是 Java 9 还有很多其他新功能,这篇文字会将重点介绍开发人员特别感兴趣的几种功能. 这篇文章也是 Java 新特性 ...
- Linux 内存分析工具——free命令
在Linux系统经常被用作服务器系统.当服务器内存吃紧的时候,free命令是我们最常使用的内存分析工具. free使用介绍 free命令可以显示Linux系统中空闲的.已用的物理内存及swap内存,及 ...
- [redis读书笔记] 第二部分 集群
1. 一个集群会包含多个节点(一个节点就是一个reid是服务器),CLUST MEET <ip><port>可以添加一个node到集群,命令执行后,两个node之间就会进行握手 ...
- 查看 Linux 中文件打开情况(lsof)
前言 我们都知道,在linux下,“一切皆文件”,因此有时候查看文件的打开情况,就显得格外重要,而这里有一个命令能够在这件事上很好的帮助我们-它就是lsof. Linux 下有哪些文件 在介绍lsof ...
- [ERROR]pip insall pyodbc
- 错误代码如下: pip install pyodbc Looking in indexes: http://172.16.1.250/repository/douban/simple Collec ...
- 杭电-------2055An Easy Problem(C语言)
#include<stdio.h> int main() { int m; int i; scanf("%d", &m); ]; int y; int z; ; ...