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<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>vec[1005];
vector<int>v;
int num[1005];
int main()
{int n,m,a,b;scanf("%d%d",&n,&m);for(int i = 1;i<=m;i++){scanf("%d%d",&a,&b);vec[a].push_back(b);num[b]++;//表明完成这个任务需要先完成哪些 }int k;scanf("%d",&k);int dd[1005],gg[1005];for(int p = 0; p < k ; p++){for(int i = 1;i <= n ;i++){dd[i] = num[i];scanf("%d",&gg[i]); }for(int i = 1;i <= n; i++){if(dd[gg[i]] != 0){v.push_back(p); break;}else  //表示他等于0 了 那么把他vector里面的东西各自减一 {int size = vec[gg[i]].size();for(int j = 0;j < size; j++)dd[ vec[gg[i]][j] ]--;}}   }int o = v.size();for(int i = 0;i<o;i++)printf("%d%c",v[i]," \n"[i==o-1]);return 0;
}

1146 Topological Order(25 分)相关推荐

  1. PAT甲级1146 Topological Order :[C++题解]拓扑排序、结构体存边、图论

    文章目录 题目分析 题目链接 题目分析 来源:acwing 拓扑排序是对有向无环图的顶点的一种排序,它使得若存在一条从顶点A到顶点B的路径A→B,则在排序中顶点B出现在顶点A的后面. 根据概念,可以想 ...

  2. 1146 Topological Order

    题目 题意:在给定有向图中,对于给定查询序列是否是有向图中的一个拓扑序列,记录非法序列下标 tip:模拟拓扑排序 #include<iostream> #include<vector ...

  3. 【PAT甲级】1146 Topological Order

    ✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343

  4. 【PAT (Advanced Level) Practice】1051 Pop Sequence (25 分)

    1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order ...

  5. 1121 Damn Single (25 分)

    1121 Damn Single (25 分) "Damn Single (单身狗)" is the Chinese nickname for someone who is bei ...

  6. 7-3 堆中的路径 (25 分)

    7-3 堆中的路径 (25 分) 将一系列给定数字插入一个初始为空的小顶堆H[].随后对任意给定的下标i,打印从H[i]到根结点的路径. 输入格式: 每组测试第1行包含2个正整数N和M(≤1000), ...

  7. C++学习之路 | PTA(甲级)—— 1114 Family Property (25分)(带注释)(并查集)(精简)

    1114 Family Property (25分) This time, you are supposed to help us collect the data for family-owned ...

  8. 1067 Sort with Swap(0, i) (25 分)

    1067 Sort with Swap(0, i) (25 分) Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy ...

  9. PAT乙级1025反转链表 25(分)

    1025 反转链表 (25 分) 给定一个常数 K 以及一个单链表 L,请编写程序将 L 中每 K 个结点反转.例如:给定 L 为 1→2→3→4→5→6,K 为 3,则输出应该为3→2→1→6→5→ ...

  10. 【PAT甲】1051 Pop Sequence (25分)判断出栈顺序的合法性

    problem 1051 Pop Sequence (25分) Given a stack which can keep M numbers at most. Push N numbers in th ...

最新文章

  1. 【运维】阿里云宝塔面板部署JavaWeb项目
  2. opencv标定详细用法
  3. 一凌网抢攻在线市场 移动办公引领管理新热潮
  4. Spring Boot 2.0正式发布,升还是不升呢?
  5. 云服务时代,你是否没穿内裤在到处逛啊!
  6. 设计模式之桥接模式实例
  7. ViewPager留两边 显示左右两边的视图
  8. 如何用深度学习进行CT影像肺结节探测(附有基于Intel Extended Caffe的3D Faster RCNN代码开源)
  9. 包治百病 | 如何将一个.NET Core类库发布到NuGet
  10. clob字段怎么导出_Oracle 11g及12c+版本下为啥有些表不能exp导出?
  11. tomcat(17)启动tomcat
  12. 武汉街头出现手机无线充电路灯,极速快充,但需注意这一点!
  13. 手机远程服务器总说磁盘空间不足,查询远程服务器上磁盘空间的最佳方式
  14. PostgreSQL如何自动更新时间戳?
  15. ApacheCN - 关于我们
  16. 帮你一个简单的微信怎么刷票及微信如何刷票技巧「图文教程」
  17. Cygwin的安装使用,及其软件包apt-cyg的配置使用,以及apt-cyg错误“/usr/bin/apt-cyg: line 25: $‘\r‘: command not found”解决
  18. 养老保险和住房公积金如何转移
  19. FZU - 2062 - Suneast Yayamao
  20. JS中函数的返回值介绍

热门文章

  1. Java并发编程:Lock(上)
  2. 机器学习-GBDT和XGboost
  3. 《勒索软件经济分析》:“理想赎金”950英镑
  4. Linux 有问必答: 如何在Linux中加入cron任务
  5. PCA MATLAB
  6. 没有月亮的中秋,有CCNP伴我度过!
  7. linux 初步学习
  8. Elastic search相关
  9. idea里查找maven的jar包冲突
  10. Spring注解——同一接口有多个实现类,如何注入