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 ...
随机推荐
- UIWebView 禁止检测链接弹出UIActionSheet
解决方法一: 添加以下代码禁止检测类型 webView.dataDetectorTypes = UIDataDetectorTypeNone; 解决方法二: - (void)webViewDidFin ...
- SQL Server DACPAC数据库部署错误
DACPAC使用sqlpackage.exe进行部署,部署时候报错: EXEC : error SQL72035: [dbo].[table] is under change data capture ...
- java创建对象方法列表
引用 不用构造方法也能创建对象 前言 java中对象创建的方法主要包括,1,使用new关键字,2.使用clone方法,3.反射机制,4.反序列化.其中1,3都会明确的显式的调用构造函数.2是在内存上对 ...
- HTML,CSS,JS个别知识点总结
<input>是自闭合标签,没有<input></input>一说,只能写作<input/>. <div>可以是行标签也可以作为块标签,作为 ...
- Sublime如何设置背景透明
Sublime如何设置背景透明 下载sublime 透明背景插件 我用的是git下载插件: git clone https://github.com/vhanla/SublimeTextTrans.g ...
- drf 视图源码详解
目录 mixin类和Generic类 CreateModelMixin 创建 ListModelMixin - 查看多条数据 RetrieveModelMixin 获取单条数据 UpdateModel ...
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第3节 Random类_11-练习二_猜数字小游
0到100之间的数字.猜多少次才能猜对最终的结果.大了或者小了都会告诉你. 二分法查找. 循环次数不确定用whilte true的方式去循环 前两种情况是需要重试的 把猜测的代码放在whilte循环里 ...
- UI自动化之特殊处理三(日期控件\表格\富文本)
日期控件\表格\富文本也是一些常遇到的需要特殊处理的定位 目录 1.日期控件 2.表格 3.富文本 1.日期控件 第一种:输入框属性为:readonly="readonly" # ...
- 类Random
/* * Random:产生随机数的类 * * 构造方法 * public Random();没有给种子,用的是默认种子,是当前时间的毫秒值 * public Random(long seed);使用 ...
- Golang new() vs make()
对于Golang的new() 和 make()的用法有些混乱,感觉这篇资料讲解较好,翻译一下,方便学习! 原文地址:http://www.godesignpatterns.com/2014/04/ne ...