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. NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第二轮Day2题解

    肝了两题... T1一眼题,分解质因数,找出2的个数和5的个数取min输出 #include<iostream> #include<cstring> #include<c ...

  2. HDU 4303 树形DP

    Hourai Jeweled Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 163840/163840 K (Java/Others) ...

  3. is(':checked'); 判断radio是否选中,该属性会随着radio被切换点击而变化

    // var is_rec =$("#is_rec_on").is(':checked'); //判断radio是否选中,该属性会随着radio被切换点击而变化 // if(is_ ...

  4. JS常见的算法

    原文链接:Jack Pu's Blog 虽说我们很多时候前端很少有机会接触到算法.实际上学习数据结构与算法对于工程师去理解和分析问题都是有帮助的.如果将来当我们面对较为复杂的问题,这些基础知识的积累可 ...

  5. [洛谷P2610] [ZJOI2012]旅游

    洛谷题目链接:[ZJOI2012]旅游 题目描述 到了难得的暑假,为了庆祝小白在数学考试中取得的优异成绩,小蓝决定带小白出去旅游~~ 经过一番抉择,两人决定将T国作为他们的目的地.T国的国土可以用一个 ...

  6. sqlserver 个人整理

    1.创建临时表(临时表的生命周期是当前回话中) //创建临时表是使用#+表名 //判断临时表是否存在 if object_id('tempdb..#ttt1') is not null Begin d ...

  7. python测试rabbitmq简易实例

    生产者 import pika #coding=utf8 credentials = pika.PlainCredentials('guest', '密码') connection = pika.Bl ...

  8. 优雅退出telnet

    echo "" |telnet IP 端口

  9. Jmeter-Java heap内存溢出

    使用jmeter进行压力测试时遇到一段时间后报内存溢出outfmenmory错误,导致jmeter卡死了,先尝试在jmeter.bat中增加了JVM_ARGS="-Xmx2048m -Xms ...

  10. freemarker中空值“”,null值的判断

    原文:http://zhousheng193.iteye.com/blog/1319772 <#if letVo.manageScore!=""> ${html('${ ...