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 ...
随机推荐
- mybatis缓存,从一个“灵异”事件说起
刚准备下班走人,被一开发同事叫住,让帮看一个比较奇怪的问题:Mybatis同一个Mapper接口的查询方法,第一次返回与第二次返回结果不一样,百思不得其解! 问题 Talk is cheap. Sho ...
- 严重 [RMI TCP Connection(3)-127.0.0.1]
学习Servlet时碰到的一个bug. Connected to server [2017-01-08 04:40:33,100] Artifact jspRun:war exploded: Arti ...
- 【Qt学习笔记】Qt+VS2010的配置
http://blog.csdn.net/jocyln9026/article/details/8575218 关于Qt Qt是1991年由Trolltech公司开发的一个跨平台的C++图形用户界面应 ...
- 不要被C++“自动生成”所蒙骗
http://www.cnblogs.com/fanzhidongyzby/archive/2013/01/12/2858040.html C++对象可以使用两种方式进行创建:构造函数和复制构造函数. ...
- springboot整合druid和配置资源监控
1.添加依赖,在maven repository中搜索 <dependency> <groupId>com.alibaba</groupId> <artifa ...
- pyspark 记录
import os import sys spark_name = os.environ.get('SPARK_HOME',None) if not spark_name: raise ValueEr ...
- WeChall_Enlightment (Encoding)
解题: 刚开始一看这题就蒙逼了,完全没思路,过了几天后再仔细去想想,应该是二进制的ascii码,但是原来的三张图虽然都是8的倍数,但完全转换不成有用的东西,题目的意思能否找到光,百度了一下关于三原色的 ...
- 使用MS Devops 来部署CRM Solution
在D365 CE开发当中,有一个非常痛苦的问题就是开发,测试环境中的export import solution 部署问题. Devops中能很好的解决这个问题. 工作原理: 在Azure Devop ...
- 利用selenium模拟登陆
第一部:利用selenium登陆 导入selenium库 from selenium import webdriver 明确模拟浏览器在电脑中存放的位置,比如我存在当前目录 chromePath = ...
- 题解 bzoj1954【Pku3764 The xor – longest Path】
做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...