<p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>

<p>All of the tickets belong to a man who departs from <code>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>

<ul>
	<li>For example, the itinerary <code>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</code>.</li>
</ul>

<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]]
<strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]]
<strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;]
<strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= tickets.length &lt;= 300</code></li>
	<li><code>tickets[i].length == 2</code></li>
	<li><code>from<sub>i</sub>.length == 3</code></li>
	<li><code>to<sub>i</sub>.length == 3</code></li>
	<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
	<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>

class Solution {
public:
    vector<string> findItinerary(vector<vector<string>> tickets) {

        // find the unique nodes
        set<string> nodesUnique;
        for (auto &e: tickets){
            nodesUnique.insert(e[0]);
            nodesUnique.insert(e[1]);
        }

        // prepare mappings from str to integer IDs - so we can apply regular DFS
        vector<string> nodes2str(nodesUnique.size());
        int n = 0;
        for (auto &i : nodesUnique) nodes2str[n++] = i;
        map<string, int> str2nodes;
        for (int i = 0; i < n; i++) str2nodes[nodes2str[i]] = i;

        // prepare an integer adjacency list (with duplicate edges possible!).
        vector<vector<int>> adjlist(n, vector<int> ());
        vector<vector<bool>> visited(n, vector<bool> ());
        for (auto &e: tickets) adjlist[str2nodes[e[0]]].push_back(str2nodes[e[1]]);

        for (int i = 0; i < n; i++) {
            sort(begin(adjlist[i]), end(adjlist[i]));
            visited[i].resize(adjlist[i].size());
            fill(begin(visited[i]), end(visited[i]), false);
        }

        int pathlen = 0;
        vector<int> path(tickets.size() + 1);
        dfs(str2nodes["JFK"], pathlen, path, adjlist, visited);

        pathlen = 0;
        vector<string> ans(tickets.size() + 1);
        for (int i : path) ans[pathlen++] = nodes2str[i];

        return ans;
    }

    bool dfs(int node, int &pathlen, vector<int> &path, vector<vector<int>> &adjlist,
    vector<vector<bool>> &visited) {

        // add node to the path
        path[pathlen++] = node;
        if (pathlen == path.size()) return true;

        for (int i = 0; i < adjlist[node].size(); i++) {
            if (!visited[node][i]) {
                visited[node][i] = true;
                if(dfs(adjlist[node][i], pathlen, path, adjlist, visited)) return true;
                else {
                    int j = i + 1;
                    while (j < adjlist[node].size() and adjlist[node][j] == adjlist[node][i]) j++;
                    visited[node][i] = false;
                    i = j - 1;
                }
            }
        }
        
        pathlen--;
        return false;
    }
};

auto init = [](){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    return 0;
}();
18600