当前位置:网站首页>剑指 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;
}
};边栏推荐
- 能量原理与变分法笔记17:广义变分原理(识别因子方法)
- Introduction to web security SSH testing and defense
- Leetcode 238. product of arrays other than itself
- 2022 Shandong elderly care exhibition, China International elderly care service industry exhibition, Jinan elderly care industry exhibition
- 编译器LLVM-MLIR-Intrinics-llvm backend-instruction
- 安装Win11找不到固态硬盘如何解决?
- PostgreSQL sequence cache 参数导致的数值序列不连续 有间隔 gap
- Osgearth2.8 compiling silvering cloud effect
- MySQL data recovery - using the data directory
- Mecol Studio - Little Bear Development Notes 3
猜你喜欢
![Relevant interfaces of [asp.net core] option mode](/img/2e/847e7541cfc49fd69794089dce2df2.jpg)
Relevant interfaces of [asp.net core] option mode

17.生命周期

C language leak detection and filling (1)

如何给电脑系统重置系统?方法其实很简单
![[unity project practice] level unlocking](/img/14/a12ad9aa7741599222aa4db8688713.png)
[unity project practice] level unlocking

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

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

2022上半年中国十大收缩行业

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

Task03 | return
随机推荐
Detailed writing process of impala
2022山东养老展,中国国际养老服务业展览会,济南老龄产业展
安装svn 汉化包 也不能设置中文[通俗易懂]
dokcer镜像理解
Baidu map data visualization
task03笔记2
[深入研究4G/5G/6G专题-40]: URLLC-11-《3GPP URLLC相关协议、规范、技术原理深度解读》-5-5G Qos原理与架构: 切片、PDU会话、QosFlow、5QI、DRB
Leetcode 228. summary interval (yes, solved)
安装Win11找不到固态硬盘如何解决?
How important is 5g dual card and dual access?
QT with OpenGL (frame cache)
【Unity项目实践】委托
[untitled]
Mekol Studio - Little Bear Development Notes 2
Reduced order method of linear algebraic determinant calculation method
Leetcode 238. 除自身以外数组的乘积
干货!神经网络中的隐性稀疏正则效应
小程序頭像組樣式
New product listing | A-share floor derivatives market point
Cannot read properties of null (reading ‘pickAlgorithm‘)