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:

  1. 6 8
  2. 1 2
  3. 1 3
  4. 5 2
  5. 5 4
  6. 2 3
  7. 2 6
  8. 3 4
  9. 6 4
  10. 5
  11. 1 5 2 3 6 4
  12. 5 1 2 6 3 4
  13. 5 1 2 3 6 4
  14. 5 2 1 6 3 4
  15. 1 2 3 4 5 6

Sample Output:

  1. 3 4
  2.  
  3. 开门见山的题。
    代码:
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <map>
  5. #define Max 1005
  6. using namespace std;
  7. int n,m,k,limit[Max],exa[Max],a,b,mp[Max][Max],ans[Max],ant;///limit存某个点前面有几个点先行
  8.  
  9. int check()
  10. {
  11. int p[Max];
  12. for(int i = ;i <= n;i ++)///limit转到p
  13. {
  14. p[i] = limit[i];
  15. }
  16. for(int i = ;i < n;i ++)
  17. {
  18. if(p[exa[i]])///p为正表示顺序不合法
  19. {
  20. return ;
  21. }
  22. for(int j = ;j <= n;j ++)///如果合法 把受它限制的点 p都减1
  23. {
  24. if(mp[exa[i]][j])p[j] --;
  25. }
  26. }
  27. return ;
  28. }
  29. int main()
  30. {
  31. scanf("%d%d",&n,&m);
  32. for(int i = ;i < m;i ++)
  33. {
  34. scanf("%d%d",&a,&b);
  35. mp[a][b] = ;
  36. limit[b] ++;
  37. }
  38. scanf("%d",&k);
  39. for(int i = ;i < k;i ++)
  40. {
  41. for(int j = ;j < n;j ++)
  42. {
  43. scanf("%d",&exa[j]);
  44. }
  45. if(!check())
  46. {
  47. ans[ant ++] = i;
  48. }
  49. }
  50. for(int i = ;i < ant;i ++)
  51. {
  52. if(i)printf(" %d",ans[i]);
  53. else printf("%d",ans[i]);
  54. }
  55. }

1146. Topological Order (25)的更多相关文章

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

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

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

  3. PAT 1146 Topological Order[难]

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

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

  5. PAT 甲级 1146 Topological Order

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

  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. 1146 Topological Order

    题意:判断序列是否为拓扑序列. 思路:理解什么是拓扑排序就好了,简单题.需要注意的地方就是,因为这里要判断多个,每次判断都会改变入度indegree[],因此记得要把indegree[]留个备份.ps ...

  8. PAT_A1146#Topological Order

    Source: PAT A1146 Topological Order (25 分) Description: This is a problem given in the Graduate Entr ...

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

随机推荐

  1. eclipse jshint安装

    http://www.cnblogs.com/lhb25/archive/2012/04/05/javascript-validation-with-jshint-eclipse.html

  2. 《SQL Server 2012 T-SQL基础》读书笔记 - 8.数据修改

    Chapter 8 Data Modification SQL Server 2008开始,支持一个语句中插入多行: INSERT INTO dbo.Orders (orderid, orderdat ...

  3. 在Ubuntu16.04下安装SourceInsight和WeChat

    1 使用Wine安装SourceInsight4 1.1 安装Wine $ sudo apt-get install wine 1.2 安装SourceInsight 下载SourceInsight软 ...

  4. loj#6036 编码

    分析 考虑trie+2sat 每次将?=0和?=1的分别插入 插入串时将这个点的选择状态和前缀的选择状态连关系边 注意串结束时建一个新点表示当前串 最后跑2sat即可 代码 #include<b ...

  5. 008-elasticsearch5.4.3【二】ES使用、ES客户端、索引操作【增加、删除】、文档操作【crud】

    一.ES使用,以及客户端 1.pom引用 <dependency> <groupId>org.elasticsearch.client</groupId> < ...

  6. Windows Server 2008 R2 为用户“IIS APPPOOL\DefaultAppPool”授予的权限不足,无法执行此操作

    报表开发与部署好后,也嵌入到aspx页面中了,使用VS自带的Web服务器组件,一切正常,当部署到IIS中的时,出现了如下错误: 为用户“IIS APPPOOL\DefaultAppPool”授予的权限 ...

  7. 测开之路九十七:js的引用方式

    第一种:引用外部js文件 准备一个js文件 <!-- 引用外部的js --><script src="../js/js01.js"></script& ...

  8. python 列表、字典多排序问题

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/justin051/article/det ...

  9. oracle--goldengate安装实例

    goldengate命令汉语解析地址:https://blog.csdn.net/eadela/article/details/102668879 实例地址:https://www.jianshu.c ...

  10. C语言1-2019级秋季作业第一周作业

    1.你对软件工程专业或者计算机科学与技术专业了解是怎样? 软件工程专业是指对计算机的软件方面灵活掌控,开发软件的工程.软件工程其中会用到计算机科学.数学方面构建模型与算法:软件工程的目标就是开发出能够 ...