当前位置:网站首页>Sword finger offer II 115. reconstruction sequence
Sword finger offer II 115. reconstruction sequence
2022-07-23 22:29:00 【anieoo】
Original link : The finger of the sword Offer II 115. Reconstruction sequence
solution:
Topology sorting application
const int N=100010;
int h[N],e[N],ne[N];
int n,m;
int d[N];//d Indicates the penetration of the point
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; // The queue used for topological sorting
// Set all entries to 0 To join the queue
for(int i = 0;i < n;i++) {
if(!d[nums[i]]) {
que.push(nums[i]);
}
}
int index = 0; // Topological sequence sum nums Compare
while(!que.empty()) {
if(que.size() > 1) return false; // The degree of 0 The number of is greater than 1 return 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]--;// Delete point t Point of reference j The edge of
if(d[j]==0) {
que.push(j);
}// If you order j Your penetration is zero , Just point j The team
}
}
return true;
}
};边栏推荐
- 激光雷达点云数据录制的rosbag文件转换成csv文件
- el-select下拉框多选远程搜索反显
- Inspiration from Buffett's shareholders' meeting 2021-05-06
- Microsoft SQL Server数据库语言及功能使用(十三)
- MySQL的JDBC編程
- ONEFLOW V0.8.0 officially released
- Yuanqi Digitalization: existing mode or open source innovation Lixia action
- MySQL的 DDL和DML和DQL的基本语法
- 小说里的编程 【连载之十七】元宇宙里月亮弯弯
- Neo4j application
猜你喜欢
随机推荐
Still worrying about xshell cracking, try tabby
Taoying collects goods in batches. How to save the babies that have not been uploaded and then import them later
产品生命周期,常见的项目职能,信息流 都是什么
did you register the component correctly
关于电脑端同步到手机端数据
怎么开户买收益百分之六的理财产品呢?
STM32单片机使用ADC功能驱动手指检测心跳模块
How about opening an account for Haitong Securities? Is it safe
Stm32+esp8266+mqtt protocol connects Alibaba cloud Internet of things platform
Life always needs a little passion
Memory search - DP
U++ learning notes control object scale
软件测试工作内容太简单怎么办?
Relevant interfaces of [asp.net core] option mode
DeFi项目的盈利逻辑 2021-04-26
激光雷达点云数据录制的rosbag文件转换成csv文件
Basic syntax of MySQL DDL and DML and DQL
What are the product life cycle, common project functions, and information flow
MVVM和MVVMLight简介及项目开发(一)
I, AI doctoral student, online crowdfunding research topic







