当前位置:网站首页>剑指 Offer II 115. 重建序列
剑指 Offer II 115. 重建序列
2022-07-23 19:07:00 【anieoo】
solution:
拓扑排序应用
const int N=100010;
int h[N],e[N],ne[N];
int n,m;
int d[N];//d表示点的入度
class Solution {
public:
int idx = 0;
void add(int a,int b) {
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
bool sequenceReconstruction(vector<int>& nums, vector<vector<int>>& sequences) {
memset(h, -1, sizeof(h));
memset(e, 0, sizeof(e));
memset(ne, 0, sizeof(ne));
memset(d, 0, sizeof(d));
int n = nums.size();
for (const auto& s: sequences) {
for (int i = 1; i < s.size(); ++i) {
add(s[i - 1], s[i]);
d[s[i]]++;
}
}
queue<int> que; //拓扑排序使用的队列
//将所有入度为0的点加入队列
for(int i = 0;i < n;i++) {
if(!d[nums[i]]) {
que.push(nums[i]);
}
}
int index = 0; //拓扑序列和nums进行比较
while(!que.empty()) {
if(que.size() > 1) return false; //入度为0的个数大于1返回false
int t = que.front();
if(t != nums[index++]) return false;
que.pop();
for(int i = h[t];i != -1;i = ne[i]) {
int j = e[i];
d[j]--;//删除点t指向点j的边
if(d[j]==0) {
que.push(j);
}//如果点j的入度为零了,就将点j入队
}
}
return true;
}
};边栏推荐
- Mecol Studio - Little Bear Development Notes 3
- Configure MySQL master-slave replication with mysqldump or mydumper
- 21. Mix in details
- next数值型数据类型()出现输入错误后,下次依然能正常输入
- Osgearth2.8 compiling silvering cloud effect
- 2022第四届中国国际养老服务业展览会,9月26日在济南举办
- scanf()和getchar()的用法讨论
- 千呼万唤,5G双卡双通到底有多重要?
- 2022 Shandong elderly care exhibition, China International elderly care service industry exhibition, Jinan elderly care industry exhibition
- 安装svn 汉化包 也不能设置中文[通俗易懂]
猜你喜欢

AtCoder B - Pizza

Energy principle and variational method note 12: minimum potential energy principle

AtCoder——Subtree K-th Max

深入浅出边缘云 | 1. 概述

Leetcode 152. product maximum subarray (brute force cracking can actually pass!)

MySQL data recovery - using the data directory

Failure after reinstalling the system (error: Reboot and select proper boot device or insert boot media in selected boot device)

Introduction to web security SSH testing and defense

Robot decision-making system based on self-learning (daki technology, Zhao kaiyong)

能量原理与变分法笔记17:广义变分原理(识别因子方法)
随机推荐
osgearth2.8编译siverlining云效果
Lecture 9 of project practice -- operation import and export tool
The latest version of conflict+docker+mysql8 deployment tutorial
task03笔记2
梅科尔工作室-华为14天鸿蒙设备开发实战笔记五
2022山东老博会,山东养老展,中国国际养老服务业展9月举办
新品上市|A股场内衍生品大盘点
Relevant interfaces of [asp.net core] option mode
[unity project practice] level unlocking
如何给电脑系统重置系统?方法其实很简单
使用 mysqldump 或 mydumper 配置 MySQL 主从复制
Energy principle and variational method note 18: virtual force principle
dokcer镜像理解
安装svn 汉化包 也不能设置中文[通俗易懂]
R language uses the gather function of tidyr package to convert a wide table to a long table (wide table to long table), the first parameter specifies the name of the new data column generated from th
New product listing | A-share floor derivatives market point
千呼万唤,5G双卡双通到底有多重要?
搭建自己的目标检测环境,模型配置,数据配置 MMdetection
2022DASCTF MAY
Detailed writing process of impala