当前位置:网站首页>PAT甲级 1146 拓扑顺序
PAT甲级 1146 拓扑顺序
2022-07-29 07:01:00 【键盘奏鸣曲】
这是 2018 年研究生入学考试中给出的一个问题:
以下哪个选项不是从给定的有向图中获得的拓扑序列?
现在,请你编写一个程序来测试每个选项。
输入格式
第一行包含两个整数 N 和 M,分别表示有向图的点和边的数量。接下来 M 行,每行给出一条边的起点和终点。
点的编号从 1 到 N。
再一行包含一个整数 K,表示询问次数。
接下来 K 行,每行包含一个所有点的排列。
一行中的数字用空格隔开。
输出格式
在一行中输出所有不是拓扑序列的询问序列的编号。询问序列编号从 0 开始。
行首和行尾不得有多余空格,保证存在至少一个解。
数据范围
1≤N≤1000,
1≤M≤10000,
1≤K≤100
输入样例:
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
输出样例:
3 4
我的解法:
#include <bits/stdc++.h>
using namespace std;
const int N = 1010, M = 10010;
struct Edge{
int a, b;
}e[M];
int p[N];
int n, m;
int main(){
cin >> n >> m;
for(int i = 0; i < m; i ++ ){
cin >> e[i].a >> e[i].b;
}
int k;
cin >> k;
bool is_first = true;
for(int i = 0; i < k; i ++ ){
for(int j = 1; j <= n; j ++ ){
int x;
cin >> x;
p[x] = j;
}
bool success = true;
for(int v = 0; v < m; v ++ ){
if(p[e[v].a] > p[e[v].b]){
success = false;
break;
}
}
if(!success){
if(is_first) is_first = false;
else cout << " ";
cout << i;
}
}
return 0;
}
边栏推荐
- 我想问一下,我flink作业是以upsert-kafka的方式写入数据的,但是我在mysql里面去更
- Why does ETL often become ELT or even let?
- 3-global exception handling
- Win11 system error: code execution cannot continue because ierutil.dll cannot be found. Reinstalling the program may fix this problem
- BeanUtils.setProperty()
- logback中RollingFileAppender属性简介说明
- Leetcode buckle classic problem -- 4. Find the median of two positively ordered arrays
- logback appender简介说明
- WPF interface layout must know basis
- Unity sends a post request to the golang server for parsing and returning
猜你喜欢
Win11vmware turns on the virtual machine and restarts on the blue screen and the solution that cannot be started
Kubernetes (五) ---------部署 Kubernetes Dashboard
Thinkphp6 realizes database backup
2022-07-28:以下go语言代码输出什么?A:AA;B:AB;C:BA;D:BB。 package main import ( “fmt“ ) func main() { f
Ethernet interface introduction
H3C_ Using setting default static routing priority to realize the active and standby function of export dual lines
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
0 9 布隆过滤器(Bloom Filter)
Job 7.28 file IO and standard IO
个人博客系统(附源码)
随机推荐
Redis基础篇
Gin routing, parameters, output
Scala 高阶(十):Scala中的异常处理
Does Flink support sqlserver databases? Get the changes of SQLSERVER database
2-统一返回类DTO对象
Nodejs installation tutorial
Gin template
H3C_ Using setting default static routing priority to realize the active and standby function of export dual lines
Custom events
logback appender简介说明
对Vintage分析的一些学习理解
After 4 years of development and 13K, if you want to change to automated testing, can your salary still rise···
Why does ETL often become ELT or even let?
以太网接口介绍
Paper reading (62):pointer networks
MySQL----多表查询
I'd like to ask, my flick job writes data in the way of upsert Kafka, but I'm more careful in MySQL
LevelFilter简介说明
JS chicken laying eggs and egg laying chickens. Who appeared earlier, object or function? Is function an instance of function?
Use vscode to configure Mysql to realize connection, query, and other functions