PAT 1146 Topological Order
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的更多相关文章
- PAT 1146 Topological Order[难]
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which o ...
- [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 ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
- PAT 甲级 1146 Topological Order
https://pintia.cn/problem-sets/994805342720868352/problems/994805343043829760 This is a problem give ...
- 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 ...
- 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 ...
- 1146. Topological Order (25)
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...
- 1146 Topological Order
题意:判断序列是否为拓扑序列. 思路:理解什么是拓扑排序就好了,简单题.需要注意的地方就是,因为这里要判断多个,每次判断都会改变入度indegree[],因此记得要把indegree[]留个备份.ps ...
- PAT_A1146#Topological Order
Source: PAT A1146 Topological Order (25 分) Description: This is a problem given in the Graduate Entr ...
随机推荐
- http-2.4
http-2.4 1)新特性 (1)MPM 支持运行为DSO 机制:以模块形式按需加载 (2)event MPM 生产环境可用 (3)异步读写机制 (4)支持每模块及每目录的单独日志级别定义 (5)每 ...
- bzoj 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚【dp+线段树】
设f[i]为i时刻最小花费 把牛按l升序排列,每头牛能用f[l[i]-1]+c[i]更新(l[i],r[i])的区间min,所以用线段树维护f,用排完序的每头牛来更新,最后查询E点即可 #includ ...
- Spark之RDD的定义及五大特性
RDD是分布式内存的一个抽象概念,是一种高度受限的共享内存模型,即RDD是只读的记录分区的集合,能横跨集群所有节点并行计算,是一种基于工作集的应用抽象. RDD底层存储原理:其数据分布存储于多台机器上 ...
- 公司6:JrVue重用布局
JrVue是我们基于element重新封装的一套组件库; 为了提高开发效率, 方便对代码的统一管理和维护, 我们提供了一些统一的页面布局, 以及项目常用的中小型模块 我在master新拉一个分支J ...
- 403 Frog Jump 青蛙过河
一只青蛙想要过河. 假定河流被等分为 x 个单元格,并且在每一个单元格内都有可能放有一石子(也有可能没有). 青蛙可以跳上石头,但是不可以跳入水中.给定石子的位置列表(用单元格序号升序表示), 请判定 ...
- 每天学点Linux命令: 管道| 与 xargs的区别
先看一个例子: find ./ -print | xargs grep a 输出: grep: ./: 是一个目录 ./less:abc ./afile:abcde ./afile:AaAbBcB . ...
- mybatis之多个对象自动装配问题
因为业务的需要,所以我在一个方法中植入三个对象,但是mybatis并没有自动装配,结果并不是我想的那么美好,还是报错了.报错截图如下: <select id="GetOneBillPa ...
- dubbo与springmvc的简单使用
什么是Dubbo? dubbo是阿里巴巴公司开源的高性能优秀服务框架,通过高性能的RPC(远程服务调用)实现服务的输入输出功能,可以与spring框架无缝整合: 传统的架构所有的模块都在一台服务器上, ...
- NodeJs学习记录(四)初学阶段关于app.js里的一些重要配置
app.set('views', path.join(__dirname, 'views')); 以上代码用于配置页面文件(例如 .ejs 文件)的根目录, 设置之后 访问 ./index 则等同于访 ...
- 观察者模式(observer)c++实现
1意图 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新. 2别名 依赖(Dependents), 发布-订阅(Publish-Subscribe ...