PAT_A1146#Topological Order
Source:
Description:
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
Keys:
Attention:
- 性质:若图顶点按照拓扑排序重新编号,则存储矩阵为上三角阵;
- 由性质可以推断出,若存储图的矩阵为三角阵,则存在拓扑排序,反之不一定成立;
- 拓扑排序算法本身比较简单,了解概念之后自然就可以写出了~
Code:
/*
Data: 2019-05-19 20:41:28
Problem: PAT_A1146#Topological Order
AC: 22:04 题目大意:
判别拓扑排序
输入:
第一行给出顶点数N<=1e3,和边数M<=1e4
接下来M行,给出顶点及其有向边(顶点从1~N)
接下来给出查询次数K
接下来K行给出顶点序列
输出:
按序号给出不是拓扑排序的序列(0~K-1)
*/ #include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int M=1e3+;
vector<int> grap[M],id(M),ans; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int n,m,u,v,s[M];
scanf("%d%d", &n,&m);
fill(id.begin(),id.end(),);
for(int i=; i<m; i++)
{
scanf("%d%d",&v,&u);
grap[v].push_back(u);
id[u]++;
}
scanf("%d", &m);
for(int i=; i<m; i++)
{
vector<int> d = id;
for(int j=; j<n; j++)
scanf("%d", &s[j]);
for(int j=; j<n; j++)
{
v=s[j];
if(d[v]==)
for(int k=; k<grap[v].size(); k++)
d[grap[v][k]]--;
else
{
ans.push_back(i);
break;
}
}
}
for(int i=; i<ans.size(); i++)
printf("%d%c", ans[i],i==ans.size()-?'\n':' '); return ;
}
PAT_A1146#Topological Order的更多相关文章
- A1146. Topological Order
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...
- 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 ...
- PAT 甲级 1146 Topological Order
https://pintia.cn/problem-sets/994805342720868352/problems/994805343043829760 This is a problem give ...
- 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 ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
- 1146. Topological Order (25)
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...
- PTA Is Topological Order
Write a program to test if a give sequence Seq is a topological order of a given graph Graph. Format ...
随机推荐
- 【ACM】hdu_zs3_1007_Rails_201308100802
Rails Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)Total Submissi ...
- hibernate之一对多映射
目录 第一章 课程简介 第二章 Hibernate中的单向一对多关联 2-1 一对多映射简介 2-2 hibernate的基础配置 2-3 创建HibernateUtil工具类 2-4 在mysql数 ...
- PHP array_chunk()
定义和用法 array_chunk() 函数把一个数组分割为新的数组块. 其中每个数组的长度由参数 size 决定. 可选参数 preserve_key 是一个布尔值,它指定新数组是否使用原数组相同的 ...
- Codeforces Round #305 (Div. 2) C题 (数论)
C. Mike and Frog time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- 微信被动回复用户消息-文本消息-springmvc环境下自动生成xml
微信被动回复用户消息-文本消息-springmvc环境下自动生成xml springmvc - 大牛! private Object subscribeMessage(Scan scan) { Sca ...
- Rust 中项目构建管理工具 Cargo简单介绍
cargo是Rust内置的项目管理工具.用于Rust 项目的创建.编译.执行,同一时候对项目的依赖进行管理,自己主动推断使用的第三方依赖库,进行下载和版本号升级. 一.查看 cargo 版本号 安装R ...
- poj 1256 Anagram—next_permutation的神奇应用
题意:给你一条字符串,让你输出字符串中字符的全排列,输出的顺序要按它给的奇葩的字典序. 题解:要输出全排列,暴力dfs可以过,但要注意题目的字典序以及相同字符的情况.如果用next_permutati ...
- ASP原码加密工具介绍
ASP原码加密工具介绍 总是会有非常多方法暴露ASP的原程序.造成数据库的password 路径都能够轻易被其它人搞到,所以对ASP程序实行加密处理是个不错的解决方法.以下来介绍一个工具假设大家感兴趣 ...
- IOS7中动态计算UILable的高度
.h文件 #import <UIKit/UIKit.h> @interface UILabel (ContentSize) - (CGSize)contentSize; @end .m文件 ...
- hdu1533 费用流模板
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...