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. BZOJ3427 Poi2013 Bytecomputer 【dp】

    题目链接 BZOJ3427 题解 容易发现最终序列一定是\(\{-1,0,1\}\)组成的 因为如果有一个位置不是,那么这个位置一定大于\(1\),那么上一个位置一定为\(1\),所以该位置一定加到过 ...

  2. iOS开发ARC机制下的内存管理技术要点

    转载一篇: iOS开发ARC内存管理技术要点.ARC内存管理原则总结.iOS ARC内存管理总结 ARC内存管理机制 (一)ARC的判断准则: 只要没有任何一个强指针指向该对象,该对象就会被释放. ( ...

  3. Dumpsdecrypted

    Dumps decrypted mach-o files from encrypted iPhone applications from memory to disk. This tool is ne ...

  4. mysql中的case when 与if else

    大神说:在sql中,能用if else  就不用case  when 下面来看看,具体为什么,没有搞清楚,如果有大神知道的提供下资料: Mysql的if既可以作为表达式用,也可在存储过程中作为流程控制 ...

  5. Maven命令行窗口指定settings.xml

    maven命令行窗口指定特定settings.xml 在命令行界面指定settings.xml,命令如下: mvn install --settings c:\user\settings.xml 例如 ...

  6. ZooKeeper分层次的法定人数(十二)

    分层次的法定人数的介绍 这个文档给出一个关于怎么使用分层次的法定人数的例子.基本思路是很简单的.首先,我们把服务端分组,然后每一组一行.下一步我们分配一个权重为每一个服务端. 下面的例子展示了怎么每组 ...

  7. 一个App架构例子分析--UI层使用MVP模式;各层之间使用Otto实现通信

    一.这个App整体的架构划分: 分为四大模块:   1.app模块 2.common模块 3.domain模块 4.model模块     app模块的依赖: dependencies {     c ...

  8. modelsim10 SE 仿真lattice Xp2工程

    1.首先要建立Lattice XP2库 在modelsim10 SE启动后.首先指定Lattice Diamond 1.4 给定的仿真器库源代码编译目录: C:\lscc\diamond\1.4\ca ...

  9. 【20151105noip膜你赛】bzoj3652 bzoj3653

    题目仿佛在讽刺我... 第一题: 题解: 考虑枚举区间右端点,维护所以左到当前的 and 和 or .注意 and 每次变化至少有一个二进制位从1变 0,or 每次至少有一个位从0变 1,所以最多有l ...

  10. 2009 Round2 A Crazy Rows (模拟)

    Problem You are given an N x N matrix with 0 and 1 values. You can swap any two adjacent rows of the ...