当前位置:网站首页>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;
}边栏推荐
猜你喜欢

My personal website doesn't allow access to wechat, so I did this

CVPR2021| 基于自监督学习的多视图立体匹配 (CVPR2021)

我,28岁,测试员,10月无情被辞:想给还在学测试 的人提个醒......

gcc/g++的使用

第7节-程序的编译(预处理操作)+链接

QT basic day 2 (2) QT basic components: button class, layout class, output class, input class, container and other individual examples
Scala higher order (10): exception handling in Scala

微服务远程调用

How to establish EDI connection with Scania in Scania?

Kubernetes (五) ---------部署 Kubernetes Dashboard
随机推荐
反射reflect
如何使用gs_expansion扩展节点
vagrant box 集群 处理
Operator3 - design an operator
WPF interface layout must know basis
MySQL 高级(进阶) SQL 语句 (一)
dba
Summary of OCR optical character recognition methods
I'd like to ask, my flick job writes data in the way of upsert Kafka, but I'm more careful in MySQL
[Charles' daily problems] when you open Charles, you can't use nails
LevelFilter简介说明
After three years of outsourcing, the salary of automatic testing after job hopping is twice that of the original. The secret is
WPF nested layout case
Section 7 - compilation of programs (preprocessing operations) + links
Win11vmware turns on the virtual machine and restarts on the blue screen and the solution that cannot be started
Vmware16 create virtual machine: win11 cannot be installed
3-全局异常处理
Gin template
cdc source能读完MySqlSnapshotSplit 就退出嘛
After 4 years of development and 13K, if you want to change to automated testing, can your salary still rise···
