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 ...
随机推荐
- 毕业论文系列之基于WiFi的智能农业大棚管控系统设计代码
#include <dht11.h>//dht11库 #include <MsTimer2.h> //定时器库的 头文件 #include < ...
- Linux下静态ip的配置
------------恢复内容开始------------ TYPE=Ethernet BOOTPROTO=static#dhcp改为static,采用静态方式 DEFROUTE=yes IPV4_ ...
- 牛客练习赛34 D little w and Exchange(归纳)
题意: 给n个数,和m 问这组数是否可以构成[1, m]中的每一个数 思路: 先将a数组排序. 先算算构成前几个数需要什么,至少需要a[1]=1 需要a[2] = 1,2 在a[2] = 1的情况下a ...
- C++中STL库函数的基本运用
学了这么长时间的STL库,现在我觉得是有必要对过去的题目和所遇到的问题做一下整理了,以便于之后更好的展开练习: 一. 为什么要用STL库? 1.简单粗暴(省事). 2.便于解决复杂的问题(在贪心题目中 ...
- linux入门系列12--磁盘管理之分区、格式化与挂载
前面系列文章讲解了VI编辑器.常用命令.防火墙及网络服务管理,本篇将讲解磁盘管理相关知识. 本文将会介绍大量的Linux命令,其中有一部分在"linux入门系列5--新手必会的linux命令 ...
- Multicast
Source Specific Multicast (SSM) The multicast that you are probably familiar with (PIM sparse and de ...
- windows设置开机自启动的地方
2013-03-24 11:06 (分类:网络安全) 精心总结,这些都是可以放小木马的好地方,留意了 1.最简单的 开始→程序→启动它的位置 C:\Documents and Settings\*** ...
- Magicodes.IE基础教程之导出Pdf
原文作者:hueifeng 说明 本教程主要说明如何使用Magicodes.IE.Pdf完成Pdf收据导出 要点 导出PDF数据 自定义PDF模板 导出单据 如何批量导出单据 导出特性说明 PdfEx ...
- .net代码实现上千次ping的实现
先上代码: 多线程实现ping校验: public void PingCameraNew(List<CameraMongoDto> assetlist) { ThreadPool.SetM ...
- PG数据库常用操作
全量迁移 备份数据 $ pg_dump -h 172.19.235.145 -U <username> -d <database> > 20180704_dbpe.sql ...