当前位置: 首页 > news >正文

【PTA Advanced】1146 Topological Order(C++)

目录

题目

Input Specification:

Output Specification:

Sample Input:

Sample Output:

思路

C++ 知识UP

代码


题目

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
6
5 2 3 6 4 1
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:

0 4 5

思路

难度评级:⭐️

1. 用邻接表存储图时,所用的空间和时间都会比较少;

        邻接表可以用vector<int> list[n]的形式;

2. 判断一个序列是否是拓扑序列时,只需要判断序列中每一个顶点在当时情况下的入度是否为0,为0后,则需要将其所指向的结点的入度都-1,重复该步骤;

3. 所以顶点的入度是比较常用的性质,应该用数组去存储,这样就可以避免每次都去遍历邻接表了,节省了时间

4. "a topological order"是拓扑序列的意思

C++ 知识UP

1. 一维数组的拷贝

一维数组可以直接拷贝到一个vector类型的容器中,方法如下:

vector<int> vec(arr, arr+n);// arr是一维数组,n是arr元素个数

也可以拷贝进另一个一维数组,方法如下:

copy(begin(arr), end(arr), begin(arrCopy)); 

2. 二维数组的拷贝

copy(&arr[0][0], &arr[0][0] + m * n, &arrCopy[0][0]); 

代码

#include <iostream>
#include <vector>using namespace std;int main(int argc, char** argv) {int n,m;cin>>n>>m;int in[1001]={0};// 统计各个顶点的入度 vector<int> list[1001];// 邻接表记录图结构 for(int i=0;i<m;i++) {int a,b;cin>>a>>b;list[a].push_back(b);in[b]++;}int K;cin>>K;bool flagOfSpace=false;for(int i=0;i<K;i++) {vector<int> order(n);for(int j=0;j<n;j++) cin>>order[j];vector<int> tin(in,in+n+1);// 检查每个顶点bool flagOfOrder=true;for(int j=0;j<n;j++) {int v=order[j]; // 检查顶点v的入度是否为0if(tin[v]!=0) {flagOfOrder=false;break; } // 从v出发指向的顶点的入度统统-1for(int x:list[v]) {tin[x]--;} }if(!flagOfOrder) {if(flagOfSpace) cout<<" ";flagOfSpace=true;cout<<i;}}return 0;
}

http://www.lryc.cn/news/8778.html

相关文章:

  • 基于stm32mp157的嵌入式linux+qt项目实战物联网毕业设计选题之智慧医疗项目
  • Java实现邮件发送功能
  • springboot+vue简单对接支付宝完整流程
  • Map 查找表
  • python--石头剪刀布游戏(列表)
  • Project Caliper:目标是打造最佳VR手柄
  • 自动驾驶:BEV开山之作LSS(lift,splat,shoot)原理代码串讲
  • C# 如何实现对“属性”的扩展
  • EBS 物料属性 先后台对应关系 MTL_SYSTEM_ITEMS_B
  • MYSQL数据库-主从复制(原理及搭建)
  • 3GPP-NR Band25标准定义频点和信道(3GPP V17.7.0 (2022-12))
  • 微信小程序 之 原生开发
  • 常用vim命令和vim基本使用及Linux用户的管理,用户和组相关文件
  • 阿里云服务器部署前后端分离项目
  • 内核经典数据结构list 剖析
  • 华为OD机试 - 考优选核酸检测点(Python)| 真题+思路+考点+代码+岗位
  • 在魔改PLUS-F5280开发板上使用合封qsp iflash
  • uni-app 瀑布流
  • 华为OD机试 - 去除多余空格(Python)| 真题+思路+考点+代码+岗位
  • MyBatis 二级缓存简单使用步骤
  • kubeadmin kube-apiserver Exited 始终起不来查因记录
  • 论文投稿指南——中文核心期刊推荐(工程材料学)
  • 【动态规划】背包问题题型及方法归纳
  • 全球十大资质正规外汇期货平台排行榜(最新版汇总)
  • 使用Paramiko时遇到的一些问题
  • 数据预处理(无量纲化、缺失值、分类特征、连续特征)
  • 【C#基础】C# 运算符总结
  • 存储性能软件加速库(SPDK)
  • 微服务(五)—— 服务注册中心Consul
  • 冷冻电镜 - ChimeraX Density Map 密度图 操作