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 <cstring>
#include <cstdio>
#include <map>
#define Max 1005
using namespace std;
int n,m,k,limit[Max],exa[Max],a,b,mp[Max][Max],ans[Max],ant;///limit存某个点前面有几个点先行 int check()
{
int p[Max];
for(int i = ;i <= n;i ++)///limit转到p
{
p[i] = limit[i];
}
for(int i = ;i < n;i ++)
{
if(p[exa[i]])///p为正表示顺序不合法
{
return ;
}
for(int j = ;j <= n;j ++)///如果合法 把受它限制的点 p都减1
{
if(mp[exa[i]][j])p[j] --;
}
}
return ;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i = ;i < m;i ++)
{
scanf("%d%d",&a,&b);
mp[a][b] = ;
limit[b] ++;
}
scanf("%d",&k);
for(int i = ;i < k;i ++)
{
for(int j = ;j < n;j ++)
{
scanf("%d",&exa[j]);
}
if(!check())
{
ans[ant ++] = i;
}
}
for(int i = ;i < ant;i ++)
{
if(i)printf(" %d",ans[i]);
else printf("%d",ans[i]);
}
}

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(25 分)

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

  5. PAT 甲级 1146 Topological Order

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

  6. PAT 1146 Topological Order

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

  7. 1146 Topological Order

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

  8. PAT_A1146#Topological Order

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

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

随机推荐

  1. LDAP常见错误码

    LDAP_SUCCESS = 0 //成功 LDAP_OPERATIONS_ERROR = 1 //操作错误 LDAP_PROTOCOL_ERROR = 2 //协议错误 LDAP_TIME_LIMI ...

  2. jquery 获取 input type radio checked的元素

    .find('input:radio:checked'):.find("input[type='radio']:checked");.find("input[name=' ...

  3. NSDate 那点事

    转载自:http://my.oschina.net/yongbin45/blog/150114 NSDate对象用来表示一个具体的时间点. NSDate是一个类簇,我们所使用的NSDate对象,都是N ...

  4. SQLserver基础--连接查询、联合查询、索引

    一.子查询补充: Exists的用法:select*from haha where exists(select*from bumen where bumen.code=haha.bumen,and b ...

  5. ijkplayer阅读笔记系列<转>

    http://blog.csdn.net/peckjerry/article/details/47663275

  6. element-ui的rules全局验证

    原文:https://www.jianshu.com/p/6a29e9e51b61 rules.js var QQV = (rule, value, callback) => { debugge ...

  7. python文件操作错误解决

    1. python读取文件时提示"UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illega ...

  8. java操作solr

    <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</a ...

  9. lazyload懒加载插件

    在main.js中引入vue-lazyload插件  并使用 注册插件: import VueLazyLoad from 'vue-lazyload' Vue.use(VueLazyLoad,{ lo ...

  10. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第1节 Scanner类_5-练习二_键盘输入三个数字

    思路分析: 获取前两个数字中的看最大值,有多重写法,这里先演示第一种.三元运算符的方式