1146. Topological Order (25)
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 <cstring>
#include <cstdio>
#include <map>
#define Max 1005
using namespace std;
int n,m,k,limit[Max],exa[Max],a,b,mp[Max][Max],ans[Max],ant;///limit存某个点前面有几个点先行 int check()
{
int p[Max];
for(int i = ;i <= n;i ++)///limit转到p
{
p[i] = limit[i];
}
for(int i = ;i < n;i ++)
{
if(p[exa[i]])///p为正表示顺序不合法
{
return ;
}
for(int j = ;j <= n;j ++)///如果合法 把受它限制的点 p都减1
{
if(mp[exa[i]][j])p[j] --;
}
}
return ;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i = ;i < m;i ++)
{
scanf("%d%d",&a,&b);
mp[a][b] = ;
limit[b] ++;
}
scanf("%d",&k);
for(int i = ;i < k;i ++)
{
for(int j = ;j < n;j ++)
{
scanf("%d",&exa[j]);
}
if(!check())
{
ans[ant ++] = i;
}
}
for(int i = ;i < ant;i ++)
{
if(i)printf(" %d",ans[i]);
else printf("%d",ans[i]);
}
}
1146. Topological Order (25)的更多相关文章
- 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805343043829760 This is a problem give ...
- 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
题意:判断序列是否为拓扑序列. 思路:理解什么是拓扑排序就好了,简单题.需要注意的地方就是,因为这里要判断多个,每次判断都会改变入度indegree[],因此记得要把indegree[]留个备份.ps ...
- PAT_A1146#Topological Order
Source: PAT A1146 Topological Order (25 分) Description: This is a problem given in the Graduate Entr ...
- 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 ...
随机推荐
- Spring Cloud架构教程 (四)服务网关(路由配置)
传统路由配置 所谓的传统路由配置方式就是在不依赖于服务发现机制的情况下,通过在配置文件中具体指定每个路由表达式与服务实例的映射关系来实现API网关对外部请求的路由. 没有Eureka和Consul的服 ...
- 关于vsftpd连接出现“响应: 530 Permission denied”的坑
在设置vsftpd.conf文件中的变量 anonymous_enable=YES 需要使用用户进行登录,如果conf文件内缺少下列三行中的任何一行都需要补充完整,不然就会出现 “响应: 530 Pe ...
- 嵌入QQ聊天
<a href="http://wpa.qq.com/msgrd?V=1&Uin=1178321443&Site=http://www.nanfangjiadian.c ...
- Guava 已经学习的代码整理
Guava 已经学习的代码整理 Guava 依赖: compile group: 'com.google.guava', name: 'guava', version: '18.0' 以下是我自己在开 ...
- CSS选择器(CCS第三版)
什么是选择器? CSS选择器就是使样式找到应用对象. 简单选择器(Simple selectors) 在日常开发中,最常用的选择器,也是最基本的选择器. 元素选择器(Type selector) 针对 ...
- 如何删除由Automater创建的服务
想要设置两个实用的快捷设置(如何设置): 1.复制当前文件或者文件夹路径 2.在终端打开文件夹 然后想到可以用mac自带的自动操作这款软件,英文叫Automater.接着发现,显示路径栏后,直接就提供 ...
- How to run a function when the page is loaded?
How to run a function when the page is loaded? window.onload = codeAddress; should work - here's a d ...
- python中导入sklearn中模块提示ImportError: DLL load failed: 找不到指定的程序。
python版本:3.7 平台:windows 10 集成环境:Anaconda3.7 64位 在jupyter notebook中导入sklearn的相关模块提示ImportError: DLL l ...
- 使用 Python 实现多进程
w 使用 Python 实现多进程https://www.ibm.com/developerworks/cn/aix/library/au-multiprocessing/
- jobs的后台进程程序如何终止?
好像没有专门的jobs相关的命令来终止后台进程, 只有通过 jobs -l看 后台进程的pid, 然后用kill来终止. 摘录: (( 进程的终止 后台进程的终止: 方法一: 通过jobs命令查看jo ...