1
0
Fork 0
cp-templates/graph/hierholzer.cc

33 lines
881 B
C++
Raw Normal View History

2023-12-02 12:05:10 +08:00
/**
*
* https://leetcode.cn/problems/reconstruct-itinerary/solutions/
* LeetCode
*
*/
class Solution {
public:
unordered_map<string, priority_queue<string, vector<string>, std::greater<string>>> vec;
vector<string> stk;
void dfs(const string& curr) {
while (vec.count(curr) && vec[curr].size() > 0) {
string tmp = vec[curr].top();
vec[curr].pop();
dfs(move(tmp));
}
stk.emplace_back(curr);
}
vector<string> findItinerary(vector<vector<string>>& tickets) {
for (auto& it : tickets) {
vec[it[0]].emplace(it[1]);
}
dfs("JFK");
reverse(stk.begin(), stk.end());
return stk;
}
};