当前位置:网站首页>Leetcode 2097 - Legal rearrangement of pairs
Leetcode 2097 - Legal rearrangement of pairs
2022-07-03 01:15:00 【coding-hwz】
leetcode 2097 — Legal rearrangement of pairs
One 、 Title Description

Two 、 analysis
It's not hard to find out , This problem is actually taking different numbers as vertices in the graph , Will all pair End to end , Then find a path that accesses all edges exactly once .
3、 ... and 、 Algorithm
1、 Euler pathway
The above analysis is the same as the definition of Euler path . Relevant definitions and algorithms of Euler graph can be referred to 『 graph theory 』 Getting started and Hierholzer Algorithm .
The description of the algorithm is as follows :
(1) Realization
The following algorithm records the incoming and outgoing nodes of all points to compare :
struct Vertex {
unordered_set<int> ins;
unordered_set<int> outs;
};
vector<int> hierholzer(unordered_map<int, Vertex>& vertexes) {
// Determine the starting point of Euler circuit
int startIndex = vertexes.begin()->first;
int n = vertexes.size();
for (auto& vertex : vertexes) {
if (vertex.second.outs.size() - vertex.second.ins.size() == 1) {
startIndex = vertex.first;
}
}
// Use dfs Determine the access reverse order of the loop
stack<int> s;
function<void(int)> dfs = [&](int index) {
while (1) {
if (vertexes[index].outs.empty()) {
break;
}
// Delete an adjacent edge
auto iter = vertexes[index].outs.begin();
int next = *iter;
vertexes[index].outs.erase(iter);
vertexes[next].ins.erase(index);
dfs(next);
}
// No adjacent edges , Stack vertices
if (vertexes[index].outs.empty()) {
s.push(index);
}
};
dfs(startIndex);
// Reverse order return
vector<int> ret;
while (!s.empty()) {
ret.push_back(s.top());
s.pop();
}
return ret;
}
(2) Optimize
actually , In addition to determining the starting point, we need to compare the out degree and in degree , In other cases, we only need the output of nodes . therefore , We can only keep the number of degrees , There is no need to maintain the number of degrees at all times in the process . When you only need to update the out degree node , We can use vector To optimize the deletion speed . The optimized implementation is as follows :
vector<int> hierholzer(unordered_map<int, vector<int>>& edges, unordered_map<int, int>& inDegrees, unordered_map<int, int>& outDegrees) {
int startIndex = inDegrees.begin()->first;
for (auto& out : outDegrees) {
int index = out.first;
if (out.second > inDegrees[index]) {
startIndex = index;
break;
}
}
stack<int> s;
function<void(int)> dfs = [&](int index) {
while (!edges[index].empty()) {
auto next = edges[index].back();
edges[index].pop_back();
dfs(next);
}
s.push(index);
};
dfs(startIndex);
vector<int> ret;
while (!s.empty()) {
ret.push_back(s.top());
s.pop();
}
return ret;
}
2、 Realization
class Solution {
public:
vector<vector<int>> validArrangement(vector<vector<int>>& pairs) {
unordered_map<int, int> inDegrees, outDegrees;
unordered_map<int, vector<int>> edges;
for (auto& p : pairs) {
++outDegrees[p[0]];
++inDegrees[p[1]];
edges[p[0]].push_back(p[1]);
}
auto circle = hierholzer(edges, inDegrees, outDegrees);
int c = circle.size();
vector<vector<int>> ret(c - 1, vector<int>(2));
for (int i = 0; i < circle.size() - 1; ++i) {
ret[i][0] = circle[i];
ret[i][1] = circle[i + 1];
}
return ret;
}
};

边栏推荐
- Draw love with go+ to express love to her beloved
- [AUTOSAR eight OS]
- 【我的OpenGL学习进阶之旅】关于欧拉角、旋转顺序、旋转矩阵、四元数等知识的整理
- MySQL foundation 06 DDL
- Key wizard hit strange learning - automatic path finding back to hit strange points
- Appuyez sur l'apprentissage de l'esprit de frappe - reconnaissance des coordonnées de fond multithreadées
- 有向图的强连通分量
- 按键精灵打怪学习-自动寻路回打怪点
- Asynchronous, email and scheduled tasks
- Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
猜你喜欢

Reading and writing speed of Reza rz/g2l arm development board storage and network measurement
![leetcode:701. Insertion in binary search tree [BST insertion]](/img/bc/1dda73198488eb81b49be2c1dff6c2.png)
leetcode:701. Insertion in binary search tree [BST insertion]

Basis of information entropy
![[AUTOSAR VI description document]](/img/3d/1382acbc4054ab218485a12b7b4e6b.png)
[AUTOSAR VI description document]
![[shutter] image component (configure local GIF image resources | load placeholder with local resources)](/img/73/19e2e0fc5ea6f05e34584ba40a452d.jpg)
[shutter] image component (configure local GIF image resources | load placeholder with local resources)
![[untitled]](/img/fd/f6b90536f10325a6fdeb68dc49c72d.png)
[untitled]

Esp32 simple speed message test of ros2 (limit frequency)

Cut point of undirected graph
![[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)](/img/ca/1d2473ae51c59b84864352eb17de94.jpg)
[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)

First hand evaluation of Reza electronics rz/g2l development board
随机推荐
Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
Excel if formula determines whether the two columns are the same
Embrace the safety concept of platform delivery
Explain the basic concepts and five attributes of RDD in detail
matlab 多普勒效应产生振动信号和处理
MySQL --- 数据库查询 - 条件查询
MySQL
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
[untitled]
Matlab finds the position of a row or column in the matrix
Telephone network problems
Kivy教程大全之 创建您的第一个kivy程序 hello word(教程含源码)
Niu Ke swipes questions and clocks in
Infrared thermography temperature detection system based on arm rk3568
MySQL基础用法02
Foundations of data science is free to download
[Androd] Gradle 使用技巧之模块依赖替换
leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】
MySQL basics 03 introduction to MySQL types
R language ggplot2 visual faceting, visual facet_wrap bar plot, using strip Text function customize the size of the strip of each facet title in the facet graph (cutimi