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 ...
随机推荐
- vue项目锚点定位+滚动定位
功能: HTML: js: scrollEvent(e) { let scrollItems = document.querySelectorAll('.condition-container') f ...
- 找实习,三本计算机 > 985文科 ?
2018年3月,大三下学期. 写了一段时间博客以后,竟有人说要内推我. 我说我大三,还没毕业,准备暑假去找实习. 网上认识的朋友建议我去春招实习试试,还有些厂在走流程中,还有机会. 我婉拒了,感觉我自 ...
- springsecurity流程梳理与总结
springsecurity的基本使用方法学习完了,还是有些懵圈,再回过头来梳理一下流程以及使用情况 1-4.传一个User实体,new一个UserPasswordAuthenticationToke ...
- SpringSecurity:hasAuthority与自定义权限校验
springsecurity中有两种权限控制方法 1.基于注解 @PreAuthorize("hasAuthority('syst:add')") 他的作用是在controller ...
- 微服务:nacos服务注册与发现
服务治理的三个角色: 服务提供者:订阅服务 服务消费者:注册服务 注册中心:记录与监控服务状态,推送服务变更信息.提供者定时发送心跳检测,心跳检测失败,就会向消费者推送变更 提供者通过负载均衡的算法选 ...
- MapGIS路网数据发布
准备 1.MapGIS 10 桌面版(我用的10.5.6.10) 2.路网的shp文件 数据导入 1.创建要素集,如果已有要素集可以不用创建: 2.导入路网要素类,选择准备好的shp文件后导入即可: ...
- 【Spring-Security】Re12 JsonWebToken
一.认证机制种类: 1.HTTP-Basic-Auth 每次请求接口必须提供账号信息[username + password] 但是信息有暴露风险,配合RestFul风格使用,逐渐淘汰 2.Cooki ...
- 分享某Python下的mpi教程 —— A Python Introduction to Parallel Programming with MPI 1.0.2 documentation ( 续 #2 )
接前文: 分享某Python下的mpi教程 -- A Python Introduction to Parallel Programming with MPI 1.0.2 documentation ...
- Committer 迎新!这次是来自阿里云的同学
截至今天,Apache DolphinScheduler 项目在 GitHub 上的 Star 数已突破 10.6K,贡献者人数也突破了 470 人.社区的不断壮大,离不开每位 Contributor ...
- 如何查看mongodb的索引命中率
如何查看mongodb的索引命中率 一.背景 现在mongodb使用率很高,经常会遇到查询慢时,就会创建索引,而有时候索引命中率又不高,下面来介绍下测试环境下如何查看索引命中率 二.方案 1.首先查看 ...