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】的更多相关文章

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

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

  3. pat 甲级 1010. Radix (25)

    1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...

  4. pat 甲级 1078. Hashing (25)

    1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...

  5. PAT 甲级 1003. Emergency (25)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  6. PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)

    1078 Hashing (25 分)   The task of this problem is simple: insert a sequence of distinct positive int ...

  7. PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)

    1070 Mooncake (25 分)   Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...

  8. PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)

    1032 Sharing (25 分)   To store English words, one method is to use linked lists and store a word let ...

  9. PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...

随机推荐

  1. Spring 官方文档笔记---Bean

    In Spring, the objects that form the backbone of your application and that are managed by the Spring ...

  2. luoguP1541 乌龟棋 题解(NOIP2010)

    P1541 乌龟棋 题目 #include<iostream> #include<cstdlib> #include<cstdio> #include<cma ...

  3. [JSOI2016]无界单词

    题目 题意:求\(\rm border\)长度为\(0\)的\(n\)位\(0,1\)字符串个数,并求字典序第\(k\)小的那一个. 首先是计数,正向不是很好算,考虑正难则反:设\(f_i\)表示长度 ...

  4. java收藏的技术资料链接

    TCP三次握手详解: https://blog.csdn.net/baiyan3212/article/details/81302448 ICE通信: https://blog.csdn.net/zh ...

  5. Java中 Map用法

    public static Map GetGoodTypes() { Map goodTypes=new HashMap(); goodTypes.put(1,"原材料"); go ...

  6. js常用转义字符列表

    转义字符 含义 \n 换行 \t 制表符 \b 空格 \r 回车 \f 换页符 \ 反斜杠 ' 单引号 '' 双引号 \0nnn 八进制代码 nnn 表示的字符( n 是 0 到 7 中的一个八进制数 ...

  7. 第11篇Kubernetes部署微服务电商平台

        kubernetes部署sock-shop微服务电商平台: 准备条件   确保kubernetes可以访问:reg.yunwei.edu镜像库   需要准备镜像:       部署微服务   ...

  8. Yum与RPM

    Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的服务器自动下载 ...

  9. jQuery遍历之向下遍历

    html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...

  10. xshell 挪动文件夹

    cp -rp /home/d001 /home/Documents 复制/home下d001到/home下Documents -r 是遍历目录,即复制整个目录-p 是保留原有属性 查看的命令是cat ...