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

#include<iostream> //水题, 考虑入度即可
#include<vector>
using namespace std;
int main(){
int n, m;
cin>>n>>m;
vector<vector<int>> v(n+1);
vector<int> indegree(n+1, 0);
vector<int> ans;
for(int i=0; i<m; i++){
int s, e;
cin>>s>>e;
v[s].push_back(e);
indegree[e]++;
}
int k, a;
cin>>k;
for(int i=0; i<k; i++){
vector<int> t=indegree;
int flag=0;
for(int j=0; j<n; j++){
cin>>a;
if(t[a]!=0) flag=1;
for(int p=0; p<v[a].size(); p++)
t[v[a][p]]--;
}
if(flag==1) ans.push_back(i);
}
for(int i=0; i<ans.size(); i++)
i==0?cout<<ans[i]:cout<<" "<<ans[i];
return 0;
}

PAT 1146 Topological Order的更多相关文章

  1. PAT 1146 Topological Order[难]

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

  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 (25 分)(拓扑较简单,保存入度数和出度的节点即可)

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

  4. PAT 甲级 1146 Topological Order

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

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

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

  7. 1146. Topological Order (25)

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

  8. 1146 Topological Order

    题意:判断序列是否为拓扑序列. 思路:理解什么是拓扑排序就好了,简单题.需要注意的地方就是,因为这里要判断多个,每次判断都会改变入度indegree[],因此记得要把indegree[]留个备份.ps ...

  9. PAT_A1146#Topological Order

    Source: PAT A1146 Topological Order (25 分) Description: This is a problem given in the Graduate Entr ...

随机推荐

  1. GitHub上README.md教程(copy)

    [说明:转载于http://blog.csdn.net/kaitiren/article/details/38513715] 最近对它的README.md文件颇为感兴趣.便写下这贴,帮助更多的还不会编 ...

  2. 几个SQL小知识(转)

    原文地址:http://www.cnblogs.com/wuguanglei/p/4205976.html 写在前面的话:之前做的一个项目,数据库及系统整体构架设计完成之后,和弟兄们经过一段时间的编码 ...

  3. bzoj 1753: [Usaco2005 qua]Who's in the Middle【排序】

    --这可能是早年Pascal盛行的时候考排序的吧居然还是Glod-- #include<iostream> #include<cstdio> #include<algor ...

  4. 10.11 NOIP模拟题(1)

    /* 离散化 差分 */ #include<bits/stdc++.h> #define N 4000007 using namespace std; int n,ans; int tmp ...

  5. 清北考前刷题day6下午好

    /* 贪心 负数一定不取 枚举最高位是1 且答案取为0的 位置, 更新答案. */ #include<iostream> #include<cstdio> #include&l ...

  6. JS自定义右键菜单案例

    要求:点击页面鼠标右键,阻止默认右键菜单的弹出,并弹出自定义右键菜单. 效果示例: 参考代码: <!DOCTYPE html> <html lang="zh-CN" ...

  7. springboot(三)配置日志

    github代码:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-serviceSLF ...

  8. IBatis.NET 的配置

    http://www.cnblogs.com/xiaogangqq123/archive/2011/06/29/2093250.html http://www.cnblogs.com/hjf1223/ ...

  9. win7如何设置自动关机

    如果想设置Win7按照自己意愿自动关机,而又不希望下载安装第三方软件,则可以通过以下两个方法来简单实现. 工具/原料 Windows7操作系统环境 方法1:利用cmd命令 1 打开cmd窗口. 方法一 ...

  10. ORA-01033:ORACLE initialization or shutdown in process

    Oracle遇到问题 :在PL/SQL当输入用户名和密码后 竟然出现标题上错误,我一项目数据库数据库全都没有备份,还有很多很多数据,该不会让我重装数据库吧,想到这个我汗那个流啊. 在网上查了下 看了看 ...