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 ...
随机推荐
- comet 推送消息到客户端
weiconfig: <system.web> <httpHandlers> <add path="comet_broadcast.ashx" typ ...
- MySQL下建立表
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL杂记页 回到顶级页面:PostgreSQL索引页 [作者 高健@博客园 luckyjackgao@gmail. ...
- C++实现tar包解析
tar(tape archive)是Unix和类Unix系统上文件打包工具,可以将多个文件合并为一个文件,使用tar工具打出来的包称为tar包.一般打包后的文件名后缀为".tar" ...
- CF 919 D. Substring
D. Substring 链接 题意: 在一张有向图中,定义路径的权值为路径中出现次数最多的字符出现的次数,求一条权值最大的路径.如果权值可以无限大,输出-1. 分析: 注意是一张有向图.如果存在环那 ...
- CS100.1x-lab1_word_count_student
这是CS100.1x第一个提交的有意义的作业,自己一遍做下来对PySpark的基本应用应该是可以掌握的.相关ipynb文件见我github. 这次作业的目的如题目一样--word count,作业分成 ...
- pdo的用处,用法
PDO主要是用来对数据库进行访问的.PDO扩展为PHP访问数据库定义了一个轻量级的一致接口,不同数据库在访问时,采用相同方法名称,解决了连接数据库不统一问题.PDO扩展自身并不能实现任何数据库功能,必 ...
- flask的cookie和session的简单原理
在Flask的框架中,自己已经封装了 cookie的respons,request 有存储就有读取及删除,那么就拿购物车来举例 在我们登陆的时候会有之前在购物车存放的物品.也就是说在一个地方为我们保存 ...
- SICP读书笔记 2.1
SICP CONCLUSION 让我们举起杯,祝福那些将他们的思想镶嵌在重重括号之间的Lisp程序员 ! 祝我能够突破层层代码,找到住在里计算机的神灵! 目录 1. 构造过程抽象 2. 构造数据抽象 ...
- NIKTO介绍及使用方法
1. NIKTO:perl语言开发的开源WEB安全扫描器:识别网站软件版本:搜索存在安全隐患的文件:检查服务器配置漏洞:检查WEB Application层面的安全隐患:避免404误判(原因:很 ...
- CocoStuff—基于Deeplab训练数据的标定工具【三、标注工具的使用】
一.说明 本文为系列博客第三篇,主要展示COCO-Stuff 10K标注工具的使用过程及效果. 本文叙述的步骤默认在完成系列文章[二]的一些下载数据集.生成超像素处理文件的步骤,如果过程中有提示缺少那 ...