This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topological order obtained from the given directed graph? Now you are supposed to write a program to test each of the options.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (≤ 1,000), the number of vertices in the graph, and M (≤ 10,000), the number of directed edges. Then M lines follow, each gives the start and the end vertices of an edge. The vertices are numbered from 1 to N. After the graph, there is another positive integer K (≤ 100). Then K lines of query follow, each gives a permutation of all the vertices. All the numbers in a line are separated by a space.

Output Specification:

Print in a line all the indices of queries which correspond to "NOT a topological order". The indices start from zero. All the numbers are separated by a space, and there must no extra space at the beginning or the end of the line. It is graranteed that there is at least one answer.

Sample Input:

6 8
1 2
1 3
5 2
5 4
2 3
2 6
3 4
6 4
5
1 5 2 3 6 4
5 1 2 6 3 4
5 1 2 3 6 4
5 2 1 6 3 4
1 2 3 4 5 6

Sample Output:

3 4
#include<iostream>
#include<cstdio>
using namespace std;
int G[][] = {}, dele[] = {};
int N, M, K;
int main(){
scanf("%d%d", &N, &M);
for(int i = ; i < M; i++){
int v1, v2;
scanf("%d%d", &v1, &v2);
G[v1][v2] = ;
}
scanf("%d", &K);
int ans[], pt = ;
for(int i = ; i < K; i++){
fill(dele, dele + , );
int isTopl = ;
for(int j = ; j <= N; j++){
int v;
int tag = ;
scanf("%d", &v);
for(int k = ; k <= N; k++){
if(dele[k] == && G[k][v] != ){
tag = ;
break;
}
}
if(tag == ){
isTopl = ;
}else{
dele[v] = ;
}
}
if(isTopl == ){
ans[pt++] = i;
}
}
for(int i = ; i < pt; i++){
if(i == pt - )
printf("%d", ans[i]);
else printf("%d ", ans[i]);
}
cin >> N;
}

总结:

1、题意:给出一个有向图,检验给出的序列是否是拓扑排序。

2、拓扑排序要求每次删除一个入度为0的节点。

A1146. Topological Order的更多相关文章

  1. 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 ...

  2. PAT_A1146#Topological Order

    Source: PAT A1146 Topological Order (25 分) Description: This is a problem given in the Graduate Entr ...

  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 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

  8. 1146. Topological Order (25)

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  9. PTA Is Topological Order

    Write a program to test if a give sequence Seq is a topological order of a given graph Graph. Format ...

随机推荐

  1. flutter 动画双指放大图片

    class GridAnimation extends StatefulWidget { @override State<StatefulWidget> createState() { r ...

  2. Android——线程通讯 Handler、Looper、Message;

    线程通讯问题 (主要用到了Handler类,Looper类和Message类以及MessageQueue) 在Android中主线程如何向子线程中发送消息的问题.让我们来想想,这其中的过程,无非就是创 ...

  3. GlusterFS 安装配置

    1.磁盘格式化 mkfs.xfs -i size=512 /dev/vdb1 mkdir -p /data/brick1 cat > /etc/fstab <<EOF /dev/vd ...

  4. 简单比较init-method,afterPropertiesSet和BeanPostProcessor

    一.简单介绍 1.init-method方法,初始化bean的时候执行,可以针对某个具体的bean进行配置.init-method需要在applicationContext.xml配置文档中bean的 ...

  5. APP test

    在讲APP测试之前,先讲一下,目前APP的操作系统以及APP相关基础知识. 一.APP基础知识 1.操作系统# 现在移动端的操作系统主流的分为两种:(1)安卓系统 (2)IOS系统. 2.安卓系统# ...

  6. 取得数据表中前N条记录,某列重复的话只取第一条记录

    项目需要筛选出不重复数据,以前没有做过,第一反应就是利用distinct处理,但是弄了好久也没搞出来,大家有知道的望告知下. 这次筛选没有使用distinct ,是利用group by ,利用id为唯 ...

  7. Java多线程之定时任务(Timer)

    package org.study2.javabase.ThreadsDemo.schedule; import java.util.Date; import java.util.Timer; imp ...

  8. javascript中关于value的一个小知识点(value既是属性也是变量)

    今天在学习input的value值时,发现这么一个小知识点,以前理解不太透彻 [1]以下这种情况是常见情况,会弹出“测试内容” <input type="button" va ...

  9. Editor markdown编辑器

    代码示例网址:http://pandao.github.io/editor.md/examples/index.html 引入文件 <link rel="stylesheet" ...

  10. POJ 3264-Balanced Lineup-RMQ问题

    裸RMQ问题 #include <cstdio> #include <algorithm> #include <cstring> using namespace s ...