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

题意:
给出一个有向图,并给出k个查询,输出所有不是拓扑排序的查查询。

思路:
1.记录每个点的next,入度。
2.vector拷贝的相关操作:C++ vector拷贝使用总结

题解:

 #include<cstdlib>
 #include<cstdio>
 #include<vector>
 using namespace std;
 //定义每个节点的入度和对应出去的节点
 struct node
 {
     ;
     vector<int> out;
 };
 int main() {
     int n, m;
     scanf("%d %d", &n, &m);
     vector<node> nodes(n + );
     int a, b;
     ; i < m; i++) {
         scanf("%d %d", &a, &b);
         nodes[a].out.push_back(b);
         nodes[b].in++;
     }
     int query;
     scanf("%d", &query);
     ;
     ; i < query; i++) {
         bool flag = true;
         //每次查询对nodes的副本进行修改。
         vector<node> tNodes(nodes);
         ; j < n; j++) {
             int t;
             scanf("%d", &t);
             //即使已经判断出来不是拓扑排序了,仍然需要接受剩下的输入。
             if (!flag) continue;
             ) {
                 ; k < tNodes[t].out.size(); k++) {
                     tNodes[tNodes[t].out[k]].in--;
                 }
             }
             else {
                 ) printf(" ");
                 printf("%d", i);
                 cnt++;
                 flag = false;
             }
         }
     }
     ;
 }

[PAT] 1146 Topological Order(25 分)的更多相关文章

  1. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

  2. PAT甲级——1146 Topological Order (25分)

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  3. PAT 1146 Topological Order[难]

    1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which o ...

  4. PAT 1146 Topological Order

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  5. 1146. Topological Order (25)

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  6. PTA PAT排名汇总(25 分)

    PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...

  7. PAT A1146 Topological Order (25 分)——拓扑排序,入度

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  8. PAT 甲级 1146 Topological Order

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343043829760 This is a problem give ...

  9. PAT 1051 Pop Sequence (25 分)

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

随机推荐

  1. AOJ.综合训练.2016-12-1

    友情提示:不要复制粘贴,看完解析先自己尝试写一下,不行再看代码!祝AC愉快 @_@ A. 近似值计算 题意分析 根据公式,先用含有n的代数式表示出来pi,然后计算这个近似值和题目给出来的3.14159 ...

  2. JS传递中文参数出现乱码的解决办法

    一.window.open() 乱码: JS中使用window.open("url?param="+paramvalue)传递参数出现乱码,提交的时候,客户端浏览器URL中显示参数 ...

  3. ubuntu 14.04 安装win7虚拟机

    主机OS:ubuntu 14.04 virtual box:http://download.virtualbox.org/virtualbox/5.1.28/virtualbox-5.1_5.1.28 ...

  4. Spring filter和拦截器(Interceptor)的区别和执行顺序

    转载自:http://listenup.iteye.com/blog/1559553 1.Filter过滤器只过滤jsp文件不过滤action请求解决方案 解决办法:在web.xml中将filter的 ...

  5. ZooKeeper概述(三)

    ZooKeeper:分布式应用的分布协调服务 ZooKeeper是一个为分布式应用提供的分布的开源的协调服务.它暴露一组简单的原子操作,分布式系统可以在这之上为同步,配置管理,和组和命名实现更高级的服 ...

  6. 51Nod 1050 循环数组最大子段和 | DP

    Input示例 6 -2 11 -4 13 -5 -2 Output示例 20 分析: 有两种可能,第一种为正常从[1 - n]序列中的最大子字段和:第二种为数组的total_sum - ([1-n] ...

  7. 使用Docker搭建Django,Nginx,R,Python部署环境

    转载自https://blog.csdn.net/The_One_is_all/article/details/76063968 基本环境: Ubuntu 16.10 docker 17.06.0-c ...

  8. 【51NOD】数字1的数量

    [算法]数位DP [题解]数位dp总结 之 从入门到模板 #include<cstdio> #include<algorithm> #include<cstring> ...

  9. 【BZOJ】1699 [Usaco2007 Jan]Balanced Lineup排队

    [算法]线段树 #include<cstdio> #include<cctype> #include<algorithm> using namespace std; ...

  10. Problem B. Harvest of Apples(杭电2018年多校+组合数+逆元+莫队)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 题目: 题意:求C(n,0)+C(n,1)+……+C(n,m)的值. 思路:由于t和n数值范围太 ...