首页 技术 正文
技术 2022年11月16日
0 收藏 831 点赞 3,103 浏览 1896 个字

Source:

PAT A1146 Topological Order (25 分)

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.

PAT_A1146#Topological Order

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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,892