LeetCode 332. Reconstruct Itinerary 最小欧拉路径
题意
给N个单词表示N个点,和N-1个单词对,表示可以走的路径,求字典序最小的总路径。
首先说下这么暴力DFS能过。暴力的我都不敢写= =
class Solution {
public:
vector<string> findItinerary(vector<vector<string> >& tickets) {
map<string, vector<string> > mp;
for (int i = 0; i < tickets.size(); i++) {
string from = tickets[i][0];
string to = tickets[i][1];
if (mp.find(from) == mp.end()) {
vector<string> v;
v.push_back(to);
mp[from] = v;
} else {
mp[from].push_back(to);
}
}
for (map<string, vector<string> >::iterator iter = mp.begin(); iter != mp.end(); iter++) {
sort(iter->second.begin(), iter->second.end());
}
vector<string> res;
string cur = "JFK";
res.push_back(cur);
dfs(cur, mp, res, tickets.size());
return res;
}
bool dfs(string cur, map<string, vector<string> > &mp, vector<string> &res, int n) {
if (res.size() == n + 1) return true;
if (mp.find(cur) == mp.end()) return false;
if (mp[cur].size() == 0) return false;
for (int i = 0; i < mp[cur].size(); i++) {
string nxt = mp[cur][i];
res.push_back(nxt);
mp[cur].erase(mp[cur].begin() + i);
if (dfs(nxt, mp, res, n)) return true;
mp[cur].insert(mp[cur].begin() + i, nxt);
res.pop_back();
}
return false;
}
};
然后说正解。
如果把每一个字符串当做一个点,每一个字符串对就是一条有向边。那么这么题目就是要求输出最小字典序的欧拉路径。
以下参考 https://www.cnblogs.com/TEoS/p/11376707.html
什么是欧拉路径?欧拉路径就是一条能够不重不漏地经过图上的每一条边的路径,即小学奥数中的一笔画问题。而若这条路径的起点和终点相同,则将这条路径称为欧拉回路。
如何判断一个图是否有欧拉路径呢?显然,与一笔画问题相同,一个图有欧拉路径需要以下几个条件:
- 首先,这是一个连通图
- 若是无向图,则这个图的度数为奇数的点的个数必须是0或2;若是有向图,则要么所有点的入度和出度相等,要么有且只有两个点的入度分别比出度大1和少1
上面这两个条件很好证明。查找欧拉路径前,必须先保证该图满足以上两个条件,否则直接判误即可。
查找欧拉路径的算法有Fluery算法和Hierholzer算法。下面介绍一下Hierholzer算法。
算法流程:
- 对于无向图,判断度数为奇数的点的个数,若为0,则设任意一点为起点,若为2,则从这2个点中任取一个作为起点;对于有向图,判断入度和出度不同的点的个数,若为0,则设任意一点为起点,若为2,则设入度比出度小1的点为起点,另一点为终点。具体起点的选择要视题目要求而定。
- 从起点开始进行递归:对于当前节点x,扫描与x相连的所有边,当扫描到一条(x,y)时,删除该边,并递归y。扫描完所有边后,将x加入答案队列。
- 倒序输出答案队列。(因为这里是倒序输出,我们可以用栈来存储答案,当然用双端队列也可以)
我画图理解一下这个算法,一个欧拉路径其实都是这个样子的

就是从起点到终点的路径上画几个圈。
举两个具体的例子

path = []
A --> B --> C 因为C没有再相连的边 所以把C加入路径 path=[C]
--> D --> B 因为B没有再相连的边 所以把B加入路径 path=[C, B]
D path=[C, B, D]
B path=[C, B, D, B]
A path=[C, B, D, B, A]

path = []
A --> B --> C --> B --> D 因为D没有再相连的边 所以把D加入路径 path=[D]
B path=[D, B]
C path=[D, B, C]
B path=[D, B, C, B]
A path=[D, B, C, B, A]
所以无论先遍历的那一条边都能得出正确的欧拉路径,既然题目要求字典序,那么每次选择最小字符串先处理即可。
代码
class Solution {
public:
vector<string> findItinerary(vector<vector<string> >& tickets) {
map<string, priority_queue<string,vector<string>,greater<string> > > mp;
for (int i = 0; i < tickets.size(); i++) {
string from = tickets[i][0];
string to = tickets[i][1];
if (mp.find(from) == mp.end()) {
priority_queue<string,vector<string>,greater<string> > q;
q.push(to);
mp[from] = q;
} else {
mp[from].push(to);
}
}
vector<string> res;
string cur = "JFK";
dfs(cur, mp, res);
reverse(res.begin(), res.end());
return res;
}
void dfs(string cur, map<string, priority_queue<string,vector<string>,greater<string> > > &mp, vector<string> &res) {
while(mp[cur].size()) {
string nxt = mp[cur].top();
mp[cur].pop();
dfs(nxt, mp, res);
}
res.push_back(cur);
}
};
LeetCode 332. Reconstruct Itinerary 最小欧拉路径的更多相关文章
- [leetcode]332. Reconstruct Itinerary
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
- 【LeetCode】332. Reconstruct Itinerary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 相似题目 参考资料 日期 题目地址:htt ...
- 【LeetCode】Reconstruct Itinerary(332)
1. Description Given a list of airline tickets represented by pairs of departure and arrival airport ...
- 【LeetCode】332. Reconstruct Itinerary
题目: Given a list of airline tickets represented by pairs of departure and arrival airports [from, to ...
- 332. Reconstruct Itinerary (leetcode)
1. build the graph and then dfs -- graph <String, List<String>>, (the value is sorted a ...
- 332 Reconstruct Itinerary 重建行程单
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
- 332. Reconstruct Itinerary
class Solution { public: vector<string> path; unordered_map<string, multiset<string>& ...
- [LeetCode] Reconstruct Itinerary 重建行程单
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
- LeetCode Reconstruct Itinerary
原题链接在这里:https://leetcode.com/problems/reconstruct-itinerary/ 题目: Given a list of airline tickets rep ...
- [Swift]LeetCode332. 重新安排行程 | Reconstruct Itinerary
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
随机推荐
- RHCA rh442 002 监控工具 脏页 块设备名 缓存
sar 看某一个时间的数据 sar -d 1 5 与iostat类似 计算机识别设备按编号识别 0-15预留出 8 为iscsi设备 做一个块设备名 名字不重要是给人看的,重要的是编号 8 17(主编 ...
- 全网最适合入门的面向对象编程教程:29 类和对象的Python实现-断言与防御性编程和help函数的使用
全网最适合入门的面向对象编程教程:29 类和对象的 Python 实现-断言与防御性编程和 help 函数的使用 摘要: 在 Python 中,断言是一种常用的调试工具,它允许程序员编写一条检查某个条 ...
- 7月22号python 每日一题
7月22号python 每日一题 LCR 121. 寻找目标值 - 二维数组 难度:中等 m*n 的二维数组 plants 记录了园林景观的植物排布情况,具有以下特性: 每行中,每棵植物的右侧相邻植物 ...
- 【Server - 运维】更改腾讯云数据库参数设置
购买的MySQL实例是一个屏蔽了后台设置的服务器: 默认大小写设置是使用严格区分的: 要设置忽略大小写,就要在my.cnf中更改配置参数 https://cloud.tencent.com/devel ...
- 【转载】科研写作入门 —— 聊聊Science Research Writing for non-native Speakers of English这本书
原地址: https://zhuanlan.zhihu.com/p/623882027 平行侠: 今天我们聊一聊Science Research Writing for non-native Spea ...
- “Vanilla” 在计算机科学和技术领域中的专业翻译
"Vanilla" 在计算机科学和技术领域中通常指的是某个系统或软件的"原始"或"基础"版本,即没有任何修改.扩展或定制的版本.它可以翻译为 ...
- NVIDIA具身机器人实验室 —— GEAR —— Generalist Embodied Agent Research —— NVIDIA机器人实验室
相关: https://www.youtube.com/watch?v=jbJPG2H8hn4
- 使用lanczos算法进行的预处理共轭梯度算法(Preconditioned Conjugate Gradients Method)
构造预处理矩阵M(对称正定) 下图来自:预处理共轭梯度法(1) 下图来自:预处理(Preconditioning) 根据上面的对于预处理共轭梯度法的介绍,我们可以得到使用lanczos算法进行的预处理 ...
- UBUNTU18.04 SERVER 多显卡 服务器,为防止显卡计算任务出现不意外报错,设置显卡工作状态为:设定持久模式
参考: https://www.cnblogs.com/devilmaycry812839668/p/14799016.html https://www.cnblogs.com/devilmaycry ...
- 免费领取云主机,在华为开发者空间玩转YOLOV3
摘要:YOLOv3(You Only Look Once version 3)是一种高效的目标检测算法,旨在实现快速而准确的对象检测. 本文分享自华为云社区<华为云开发者云主机体验[玩转华为云] ...