1146 Topological Order(25 分)
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 分)相关推荐
- PAT甲级1146 Topological Order :[C++题解]拓扑排序、结构体存边、图论
文章目录 题目分析 题目链接 题目分析 来源:acwing 拓扑排序是对有向无环图的顶点的一种排序,它使得若存在一条从顶点A到顶点B的路径A→B,则在排序中顶点B出现在顶点A的后面. 根据概念,可以想 ...
- 1146 Topological Order
题目 题意:在给定有向图中,对于给定查询序列是否是有向图中的一个拓扑序列,记录非法序列下标 tip:模拟拓扑排序 #include<iostream> #include<vector ...
- 【PAT甲级】1146 Topological Order
✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
- 【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 ...
- 1121 Damn Single (25 分)
1121 Damn Single (25 分) "Damn Single (单身狗)" is the Chinese nickname for someone who is bei ...
- 7-3 堆中的路径 (25 分)
7-3 堆中的路径 (25 分) 将一系列给定数字插入一个初始为空的小顶堆H[].随后对任意给定的下标i,打印从H[i]到根结点的路径. 输入格式: 每组测试第1行包含2个正整数N和M(≤1000), ...
- C++学习之路 | PTA(甲级)—— 1114 Family Property (25分)(带注释)(并查集)(精简)
1114 Family Property (25分) This time, you are supposed to help us collect the data for family-owned ...
- 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 ...
- PAT乙级1025反转链表 25(分)
1025 反转链表 (25 分) 给定一个常数 K 以及一个单链表 L,请编写程序将 L 中每 K 个结点反转.例如:给定 L 为 1→2→3→4→5→6,K 为 3,则输出应该为3→2→1→6→5→ ...
- 【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 ...
最新文章
- 【运维】阿里云宝塔面板部署JavaWeb项目
- opencv标定详细用法
- 一凌网抢攻在线市场 移动办公引领管理新热潮
- Spring Boot 2.0正式发布,升还是不升呢?
- 云服务时代,你是否没穿内裤在到处逛啊!
- 设计模式之桥接模式实例
- ViewPager留两边 显示左右两边的视图
- 如何用深度学习进行CT影像肺结节探测(附有基于Intel Extended Caffe的3D Faster RCNN代码开源)
- 包治百病 | 如何将一个.NET Core类库发布到NuGet
- clob字段怎么导出_Oracle 11g及12c+版本下为啥有些表不能exp导出?
- tomcat(17)启动tomcat
- 武汉街头出现手机无线充电路灯,极速快充,但需注意这一点!
- 手机远程服务器总说磁盘空间不足,查询远程服务器上磁盘空间的最佳方式
- PostgreSQL如何自动更新时间戳?
- ApacheCN - 关于我们
- 帮你一个简单的微信怎么刷票及微信如何刷票技巧「图文教程」
- Cygwin的安装使用,及其软件包apt-cyg的配置使用,以及apt-cyg错误“/usr/bin/apt-cyg: line 25: $‘\r‘: command not found”解决
- 养老保险和住房公积金如何转移
- FZU - 2062 - Suneast Yayamao
- JS中函数的返回值介绍