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 ...
随机推荐
- npm和yarn 命令比较
命令比较 npm init | yarn init:创建一个新包 npm run | yarn run:运行 package.json 中定义的脚本 npm test | yarn test:测试一个 ...
- python的基本认识
python的基本认识 初识python: python是一种跨平台的.开源的.免费的.解释型的高级编程语言: python的应用领域十分广泛.如web编程.图像处理.黑客编程.网络爬虫和科学计算等: ...
- 写写Redis十大类型zset的常用命令
其实这些命令官方上都有,而且可读性很强,还有汉化组翻译的http://redis.cn/commands.html,不过光是练习还是容易忘,写一写博客记录一下 从zset类型开始写||zset类型适合 ...
- 青少年CTF擂台挑战赛 2024 #Round 1
青少年CTF擂台挑战赛 2024 #Round 1 crypto 1.解个方程 题目: 欢迎来到青少年CTF,领取你的题目,进行解答吧!这是一道数学题!! p = 474356125652182661 ...
- 倒装句&强调句
倒装句 你[吃][胡萝卜]了吗? 吃胡萝卜了吗,[你]? 强调点不同 汉语常见于口语表达 英语则常见于书面用语 英语的语序是 主语 谓语(动词) 通常把谓语动词提前 1.完全倒装句 谓语部分完全放在主 ...
- jmeter forEach循环获取response参数值进行接口请求
jmeter forEach循环获取response参数值进行接口请求 注意: 一,ForEach控制器 输入变量前缀:输入正则表达式变量的引用名称即可 Start index for loop(ex ...
- HTML+JavaScript+CSS做一个界面
下面是一个web界面主要是前端没有后端功能:关于JavaScript几种比较常见的样式 Javaweb(1),html <!DOCTYPE html> <html lang=&q ...
- 【Python】Word文档操作
依赖库下载: pip install python-docx -i https://pypi.tuna.tsinghua.edu.cn/simple/ pip install docx2pdf -i ...
- 【Vue】Re17 Router 第四部分(参数传递,守卫函数)
一.案例搭建 新建Profile组件 组件写好内容后配置路由 { path : '/profile', component : () => import('../components/Profi ...
- NVIDIA机器人仿真环境 —— NVIDIA Isaac Sim 的headless模式/无头模式 —— 非桌面模式、非可视化模式
相关: https://developer.nvidia.com/isaac-sim 可视化模式,也就是在桌面系统上直接安装软件,具体地址: https://developer.nvidia.com/ ...