-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtravel_route.cpp
52 lines (44 loc) · 1.58 KB
/
travel_route.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "travel_route.h"
#include <algorithm>
void DepthFirstSearchEulerianPath(const std::string& node,
std::map<std::string, std::vector<std::string>>& adjacency_list,
std::vector<std::string>& path)
{
while (!adjacency_list[node].empty())
{
auto next_node = adjacency_list[node].back();
adjacency_list[node].pop_back();
DepthFirstSearchEulerianPath(next_node, adjacency_list, path);
}
path.push_back(node);
}
std::vector<std::string> FindEulerianPath(const std::vector<std::vector<std::string>>& edges,
const std::string& start)
{
std::map<std::string, std::vector<std::string>> adjacency_list;
// Build the adjacency list from the edges
for (const auto& ticket : edges)
{
const auto& from = ticket[0];
const auto& to = ticket[1];
adjacency_list[from].push_back(to);
}
// Sort the destinations in descending order for each source
for (auto& [fst, snd] : adjacency_list)
{
std::sort(snd.begin(), snd.end(), std::greater<std::string>());
}
std::vector<std::string> path;
// Start the DFS to find the Eulerian path
DepthFirstSearchEulerianPath(start, adjacency_list, path);
// Reverse the path to get the correct order
std::reverse(path.begin(), path.end());
return path;
}
// using namespace std;
//
// vector<string> solution(vector<vector<string>> tickets)
// {
// vector<string> eulerianPath = FindEulerianPath(tickets, "ICN");
// return eulerianPath;
// }