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.

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的更多相关文章

  1. A1146. Topological Order

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  2. 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 ...

  3. PAT 甲级 1146 Topological Order

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343043829760 This is a problem give ...

  4. PAT 1146 Topological Order[难]

    1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which o ...

  5. [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 ...

  6. PAT 1146 Topological Order

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  7. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

  8. 1146. Topological Order (25)

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  9. PTA Is Topological Order

    Write a program to test if a give sequence Seq is a topological order of a given graph Graph. Format ...

随机推荐

  1. HDU 4540

    简单得不能再简单的DP了. #include<iostream> const int inf=1<<30; using namespace std; int map[22][2 ...

  2. STM32的独立看门狗

    STM32 内 部自带了 2 个看门狗:独立看门狗(IWDG)和窗体看门狗(WWDG) STM32 的独立看门狗由内部专门的 40Khz 低速时钟驱动.即使主时钟发生问题.它也仍然 有效. 这里须要注 ...

  3. 使用c3p0与DBCP连接池,造成的MySql 8小时问题解决方式

    本文提供了对c3p0与DBCP连接池连接MySql数据库时. 8小时内无请求自己主动断开连接的解决方式.首先介绍一下我在项目(c3p0连接池)中遇到的问题,后面还提供了使用DBCP连接池的解决方式. ...

  4. luogu2278 [HNOI2003]操作系统

    题目大意 写一个程序来模拟操作系统的进程调度.假设该系统只有一个CPU,每一个进程的到达时间,执行时间和运行优先级都是已知的.其中运行优先级用自然数表示,数字越大,则优先级越高.如果一个进程到达的时候 ...

  5. hdfs du命令是算的一份数据

    As you can see, hadoop fsck and hadoop fs -dus report the effective HDFS storage space used, i.e. th ...

  6. hdu 2602(经典01背包)

    Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. Principal Component Analysis ---- PRML读书笔记

    To summarize, principal component analysis involves evaluating the mean x and the covariance matrix ...

  8. E20170821-mk

    Dimension   n. 尺寸; [复] 面积,范围; [物] 量纲; [数] 次元,度,维;

  9. HTML <!DOCTYPE>标签 各版本对应的标签是否有无

    参考来源: http://www.w3school.com.cn/tags/html_ref_dtd.asp HTML5/HTML 4.01/XHTML 元素和有效的 DTD 下面的表格列出了所有的 ...

  10. C#之单列双列集合绑定数据

    ---恢复内容开始--- 1.单列集合绑定方式 davList.DataSource=new BindingList<类型名>(集合名); 2.双列集合绑定方式 BindingSource ...