PAT 甲级 1146 Topological Order
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的更多相关文章
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
- 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[难]
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 ...
- 1146. Topological Order (25)
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...
- 1146 Topological Order
题意:判断序列是否为拓扑序列. 思路:理解什么是拓扑排序就好了,简单题.需要注意的地方就是,因为这里要判断多个,每次判断都会改变入度indegree[],因此记得要把indegree[]留个备份.ps ...
- PAT甲级目录
树(23) 备注 1004 Counting Leaves 1020 Tree Traversals 1043 Is It a Binary Search Tree 判断BST,BST的性质 ...
- 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 ...
随机推荐
- JavaWeb总结(十四)
无脚本JSP表达式语言EL JSP页面中显示结果 jsp:useBean和jsp:getProperty两个元素冗长而笨拙 jsp:getProperty只支持对简单Bean属性的访问 public ...
- PHP 练习(投票)
1.建立数据库 表1:DiaoYanTiMu 表2:DiaoYanXuanXiang 2.页面 页面1:投票首页 <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- 【Menu】 目录索引
一. 开发语言(25%) 1.Python 2.shell 3.C.C++ 4.java 5.html 二.系统(25%) 1.redhat Linux 2.suse Linux 3.windows ...
- 很详细的SpringBoot整合UEditor教程
很详细的SpringBoot整合UEditor教程 2017年04月10日 20:27:21 小宝2333 阅读数:21529 版权声明:本文为博主原创文章,未经博主允许不得转载. https: ...
- Python_sklearn机器学习库学习笔记(四)decision_tree(决策树)
# 决策树 import pandas as pd from sklearn.tree import DecisionTreeClassifier from sklearn.cross_validat ...
- java实现哈弗曼树
O(∩_∩)O~~ 概述 我想学过数据结构的小伙伴一定都认识哈弗曼,这位大神发明了大名鼎鼎的“最优二叉树”,为了纪念他呢,我们称之为“哈弗曼树”.哈弗曼树可以用于哈弗曼编码,编码的话学问可就大了,比如 ...
- EVA无法连接
EVA在11月19日更新后,发现DMS无法与EVA进行链接,在DMS中EVA连接测试报告中有如下报错: 根本原因 解决方法/修复 1.在本地电脑系统盘中查找文件夹“.eva-prod”, 并拷 ...
- jenkins+Gitlab持续集成环境配置教程
环境简介: Jenkins 2.156(本地win10) GitLab Enterprise Edition 10.1.4-ee (远程服务器) Apache Ant 1.9.13 (本地win10) ...
- LintCode——颜色分类
颜色分类:给定一个包含红,白,蓝且长度为 n 的数组,将数组元素进行分类使相同颜色的元素相邻,并按照红.白.蓝的顺序进行排序. 我们可以使用整数 0,1 和 2 分别代表红,白,蓝. 注意事项: 不能 ...
- js数组的比较
如果两个数组元素个数都相等,但排序不同,那么它两个相等吗?结果肯定是否定的.但如果先调用sort()方法进行排序,结果就是true了. console.log(a.sort().toString()= ...