https://pintia.cn/problem-sets/994805342720868352/problems/994805343043829760

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 <bits/stdc++.h>
using namespace std; int N, M, K;
int topo[1010][1010];
int num[1010];
map<int, int> mp; int main() {
scanf("%d%d", &N, &M);
memset(topo, 0, sizeof(topo));
while(M --) {
int a, b;
scanf("%d%d", &a, &b);
topo[a][b] = 1;
} scanf("%d", &K);
vector<int> ans;
for(int k = 0; k < K; k ++) {
bool flag = true;
for(int i = 0; i < N; i ++)
scanf("%d", &num[i]); for(int i = N - 1; i >= 1; i --) {
for(int j = i - 1; j >= 0; j --) {
if(topo[num[i]][num[j]]) {
flag = false;
break;
}
}
}
if(!flag) ans.push_back(k);
} for(int i = 0; i < ans.size(); i ++)
printf("%d%s", ans[i], i != ans.size() - 1 ? " " : "\n");
return 0;
}

  建立有向图 输入的每一组数据从后向前暴力如果走得通的话就是 false

FHFHFH

PAT 甲级 1146 Topological Order的更多相关文章

  1. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

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

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

  3. PAT 1146 Topological Order[难]

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

  4. [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 ...

  5. PAT 1146 Topological Order

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

  6. 1146. Topological Order (25)

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

  7. 1146 Topological Order

    题意:判断序列是否为拓扑序列. 思路:理解什么是拓扑排序就好了,简单题.需要注意的地方就是,因为这里要判断多个,每次判断都会改变入度indegree[],因此记得要把indegree[]留个备份.ps ...

  8. PAT甲级目录

    树(23) 备注 1004 Counting Leaves   1020 Tree Traversals   1043 Is It a Binary Search Tree 判断BST,BST的性质 ...

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

随机推荐

  1. python基础学习1-类属性访问

    #!/usr/bin/env python # -*- coding:utf-8 -*- #====> __setattr__ 重写 设置类对象属性值时候调用的魔法方法 __getattr__( ...

  2. python之打包、发布模块

    一.python中针对于写好的模块,并且比人也可以使用改模块,这样就可以以同意的打出来,让别人安装或者赋值过后可以更好的使用以及集成. 二.最近在学习python所以这里主要是记录一下python的打 ...

  3. java 迭代器只遍历了一次的解决方案

    /** * 注意:因为迭代器只能遍历一次 所以每次用完都要重新填充一次 否则会出现只替换了一次检查配置项的情况 * templateJsonIterator = templateJsonObject. ...

  4. SSIS 发送邮件

    在SSIS中Send Mail的方法主要有三种,使用Send Mail Task,使用Script Task和使用存储过程msdb.dbo.sp_send_dbmail. 一,使用Send Mail ...

  5. 常见面试算法题JS实现-设计一个有getMin功能的栈

    前言: 已经确定工作了-下周一正式入职,按理说应该是可以好好浪荡一周的,但是内心总是不安,总觉得自己这个水平真的太菜了,还是趁着现在有自己的时间,赶紧多看看书,多学习学习吧orz所以把之前校招买的书, ...

  6. css布局笔记(二)Flex

    flex Flex是"Flexible Box"的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性. 任何一个容器都可指定为Flex布局. .box{di ...

  7. Python读取文件编码解码问题

    用chardet检测编码 import chardet raw = open("model.json", 'rb').read() result = chardet.detect( ...

  8. Unity3D之AR开发(一)

    近期研究了下AR技术,下面给大家分享一下. 第一种方法:高通AR(Vuforia) Vuforia插件下载地址(官网): https://developer.vuforia.com/downloads ...

  9. Unity编辑器扩展 Chapter7--使用ScriptableObject持久化存储数据

    Unity编辑器扩展 Chapter7--使用ScriptableObject持久化存储数据 unity unity Editor ScirptableObject  Unity编辑器扩展 Chapt ...

  10. OpenFastPath(2):原生态Linux Socket应用如何移植到OpenFastPath上?

    版本信息: ODP(Open Data Plane): 1.19.0.2 OFP(Open Fast Path): 3.0.0 1.存在的问题 OpenFastPath作为一个开源的用户态TCP/IP ...