1146 Topological Order (25 分)
 

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

题意:

做这题之前首先要先去了解什么是拓扑排序,可以参考https://blog.csdn.net/qq_35644234/article/details/60578189

给出一个图,再给几组数据,让你判断这几组数据是否符合拓扑排序

题解:

保存入度数和出度的节点。用一个数组来统计每个点的入度,vector保存出度的节点,然后就可以开始判断。在判断的时候,将与这个点去掉,就是指这个点连接的所有点的入度都减了1。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int n,m,u,v;
int in[],inx[];
vector<int>out[];
int main(){
cin>>n>>m;
memset(in,,sizeof(in));
for(int i=;i<=m;i++){
cin>>u>>v;
out[u].push_back(v);//保存出去的节点
in[v]++; //计算入度
}
int k;
cin>>k;
int a[];
int num=;
for(int i=;i<k;i++){
int f=;
memcpy(inx, in, sizeof(in));//将in拷贝给inx
for(int j=;j<=n;j++){
cin>>u;
if(inx[u]!=||f==){
f=;
continue;
}
for(int p=;p<out[u].size();p++){//对受影响的节点的入度--
inx[out[u].at(p)]--;
}
}
if(!f){
a[++num]=i;
}
}
for(int i=;i<=num;i++){
cout<<a[i];
if(i!=num) cout<<" ";
}
return ;
}

PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)的更多相关文章

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

  2. PAT 甲级 1146 Topological Order

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

  3. PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)

    1048 Find Coins (25 分)   Eva loves to collect coins from all over the universe, including some other ...

  4. PAT 甲级 1037 Magic Coupon (25 分) (较简单,贪心)

    1037 Magic Coupon (25 分)   The magic shop in Mars is offering some magic coupons. Each coupon has an ...

  5. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  6. PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)

    1059 Prime Factors (25 分)   Given any positive integer N, you are supposed to find all of its prime ...

  7. PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)

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

  8. PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

    1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to i ...

  9. PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)

    1021 Deepest Root (25 分)   A graph which is connected and acyclic can be considered a tree. The heig ...

随机推荐

  1. Eclipse中修改了项目,导入Tomcat中时,括号显示原来项目的名字

    Eclipse中Tomcat导入项目并且修改了项目名字,把项目添加到Tomcat上,发现在项目后面带了个括号里面显示原来项目的名字,并且在访问的时候也只能用原来的项目名访问,怎么办呢? 1.打开你的项 ...

  2. Linux - 运行 django 时 :django.db.utils.Notsupportederror: urls not supported

    运行 django 是异常:django.db.utils.Notsupportederror: urls not supported 原因:sqlite3版本3.7的问题 解决:直接改源码 1. p ...

  3. C++编程习惯

    1.初始化列表,尽量使用. 2.函数是否加const,只用而不改变就推荐加上const.如自定义的get某个属性的函数. 3.函数参数尽量用引用传递,返回值也优先考虑引用类型(引用必须保证在使用前,本 ...

  4. sublime test 3 配置安装fortran开发环境

    1.ST3下安装包管理工具Package Control https://jingyan.baidu.com/article/3c343ff7dca2b10d3779633b.html ST主界面下c ...

  5. 04_(终结版)通过App实现对数据库的增删改

    设计思路:用户注册登录:用户注册或登录(login数据表),成功后进入增删改查(words数据表)注意:只有登录验证成功后才可以增删改查,否则提示未登录! 增:用户在App上add(单词.词义.音标) ...

  6. 如何选择梯度下降法中的学习速率α(Gradient Descent Learning Rate Alpha)

    梯度下降算法的任务是寻找参数θ,使之能够最小化损失函数. 那么梯度下降法中的学习速率α应该如何选择呢?通常我们画出损失函数随迭代次数增加而变化的曲线. 可能会得到如下的一条曲线,x轴表示迭代次数,y轴 ...

  7. GoCN每日新闻(2019-09-28)

     GoCN每日新闻(2019-09-28) 1. 可视化Go程序的调用图 https://truefurby.github.io/go-callvis/2. Go modules编写和发布官方教程 h ...

  8. Pytest权威教程21-API参考-01-函数(Functions)

    目录 函数(Functions) pytest.approx pytest.fail pytest.skip pytest.importorskip pytest.xfail pytest.exit ...

  9. SpringCloud:入门介绍

    1.微服务简介 业界大牛马丁.福勒(Martin Fowler) 这样描述微服务: 论文网址:            https://martinfowler.com/articles/microse ...

  10. kubernetes 1.14安装部署EFK日志收集系统

    简单介绍: EFK 组合插件是k8s项目的一个日志解决方案,它包括三个组件:Elasticsearch, Fluentd, Kibana.相对于ELK这样的架构,k8s官方推行了EFK,可能Fluen ...