PAT甲级——A1146 TopologicalOrder【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
Solution:
使用邻接矩阵来保存这个有向矩阵,并且把每个节点的入度计算,遍历判断的序列,每经过一个节点就判断该节点入度是不是为0,若不是,说明不是拓扑序列
每经过一个节点,将其指向节点的入度-1,表明指向节点的父节点遍历完毕,从而保证了整个序列是个拓扑序列
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std; int main()
{
int n, m, k;
cin >> n >> m;
vector<vector<int>>v(n + );
vector<int>in(n + , ), temp, res;//节点的入度
for (int i = ; i < m; ++i)
{
int a, b;
cin >> a >> b;
v[a].push_back(b);
in[b]++;
}
cin >> k;
for (int i = ; i < k; ++i)
{
bool flag = true;
temp = in;
for (int j = ; j < n; ++j)
{
int x;
cin >> x;
if (temp[x] != )flag = false;
for (auto a : v[x])--temp[a];//出现一次入度减一
}
if (!flag)
res.push_back(i);
}
for (int i = ; i < res.size(); ++i)
cout << (i == ? "" : " ") << res[i];
return ;
}
PAT甲级——A1146 TopologicalOrder【25】的更多相关文章
- PAT 甲级 1010 Radix (25)(25 分)进制匹配(听说要用二分,历经坎坷,终于AC)
1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...
- PAT 甲级1003 Emergency (25)(25 分)(Dikjstra,也可以自己到自己!)
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- pat 甲级 1010. Radix (25)
1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...
- pat 甲级 1078. Hashing (25)
1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...
- PAT 甲级 1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)
1032 Sharing (25 分) To store English words, one method is to use linked lists and store a word let ...
- PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
随机推荐
- spring data jpa 一对多查询
在一对多关系中,我们习惯把一的一方称之为主表,把多的一方称之为从表.在数据库中建立一对多的关系,需要使用数据库的外键约束. 什么是外键? 指的是从表中有一列,取值参照主表的主键,这一列就是外键. pa ...
- VC++实现窗口置顶
最近在跟着Visual C++网络编程开发与实战视频教程做HttpSourceViewer这个MFC项目时,可以看我Github上的项目HttpSourceViewer,目前基本实现了所有功能,就是关 ...
- 刚安装的程序要卸载,如何Ubuntu查看程序安装记录
如果新装一个程序,突然发现需要卸载,又忘记了程序名字,怎么解决呢? /var/log/apt/history.log /var/log/apt/term.log /var/log/aptitude 看 ...
- (4.10)sql server导入导出
SQL Server快速导入数据分享 核心参考:官网~~https://docs.microsoft.com/zh-cn/sql/relational-databases/import-export/ ...
- Vue是如何渲染页面的,渲染过程以及原理代码
Vue是如何渲染页面的,渲染过程以及原理代码:https://www.cnblogs.com/ypinchina/p/7238402.html
- maven项目使用tomcat启动报错:Server Tomcat v8.5 Server at localhost failed to start
背景说明:1)该项目为maven项目,使用的maven的本地仓库里有不少之前使用过下载的jar包: 2)从svn下载该项目后,无报错情况: 3)部署到tomcat启动报错 如下 : 4)在网上搜索了很 ...
- 2019CCPC网络赛 HDU6705 - path K短路
题意:给出n个点m条边的有向图,问图上第K短路的长度是多少(这里的路可以经过任何重复点重复边). 解法:解法参考https://blog.csdn.net/Ratina/article/details ...
- C# string.Format json格式字符串报错”输入字符串的格式不正确“
当我们在string.Format中传入Json字符串时,会报”输入字符串的格式不正确“,这是因为json的"{"符号的问题,最开始我是想着用转义一下"{",但 ...
- 开发react 应用最好用的脚手架 create-react-app
安装 npx create-react-app my-app cd my-app npm start 安装后之后,就是这样的了 配置 这样的”零配置”没法满足我们的需求,我们需要自定义,需要加一些 l ...
- openwrt 编译支持sqlite3
编译版本加载lib库 ------------------------------Libraries----------------------------------- Filesystem -- ...