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 4Solution:
使用邻接矩阵来保存这个有向矩阵,并且把每个节点的入度计算,遍历判断的序列,每经过一个节点就判断该节点入度是不是为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 ...
随机推荐
- Linux(Ubuntu)常用命令(二)
归档管理: 打包: tar -cvf xxx.tar 打包对象 (一般来说就是 -cvf 一起用)但这种不压缩的打包通常不用,接下来会说. -options:-c 生成档案文件,创建打包文件. ...
- 如何保存android app日志
android 手机日志保存方法如下: 前置条件:已安装adb 1,手机usb连接电脑,打开USB调试模式(注意仅连接一台手机设备) 2,win+R输入cmd打开命令窗口,输入指令:adb devic ...
- T1215:迷宫
[题目描述] 一天Extense在森林里探险的时候不小心走入了一个迷宫,迷宫可以看成是由n * n的格点组成,每个格点只有2种状态,.和#,前者表示可以通行后者表示不能通行.同时当Extense处在某 ...
- vscode 配置 golang开发环境
如果你使用golang,那么强烈建议你采用vscode作为IDE. 1. 首先在vscode 当中安装go插件,如上图 2. 配置 %AppData%\Code\User\settings.json ...
- 关于Java序列化你应该知道的一切
什么是序列化 我们的对象并不只是存在内存中,还需要传输网络,或者保存起来下次再加载出来用,所以需要Java序列化技术. Java序列化技术正是将对象转变成一串由二进制字节组成的数组,可以通过将二进制数 ...
- [fw]PAGE_SIZE & PAGE_SHIFT & _AC()
PAGE_SIZE & PAGE_SHIFT & _AC() 在大多系统下,PAGE_SIZE被定义为 4k 大小,即 4096 字节. 在 x86 系统里,PAGE_SIZE 和 P ...
- pychrm和linux进行链接上传代码
众享周知:现在在windows文件中我们有pycharm工具帮我们去编辑python脚本,这会省去我们大把的时间让我们进行更多的脚本编辑.有这样的一种方法,我们可以使用pycharm编辑的脚本上传到l ...
- data-*存数据,拿出ul li中的数据
<ul class="questions"> <li> <div class="question">1.您的年龄是?< ...
- Java缓冲流的优点和原理
不带缓冲的流的工作原理: 它读取到一个字节/字符,就向用户指定的路径写出去,读一个写一个,所以就慢了. 带缓冲的流的工作原理: 读取到一个字节/字符,先不输出,等凑足了缓冲的最大容量后一次性写出去,从 ...
- C++ 浅析移位运算
按位左移(<<): 按二进制形式把所有的数字向左移动对应的位数,高位移出(舍弃),低位的空位补零 按位右移(>>): 按二进制形式把所有的数字向右移动对应位移位数,低位移出(舍 ...