欧拉回路模板题
class Solution {
public:
vector<string> ans;
unordered_map<string,vector<pair<string,int>>> g;
unordered_set<int> vis;
void dfs(string s){
for(auto &[t,i]:g[s]){
if(vis.count(i)) continue;
vis.insert(i);
dfs(t);
}
ans.push_back(s);
}
vector<string> findItinerary(vector<vector<string>>& tickets) {
int n=tickets.size();
for(int i=0;i<n;i++) g[tickets[i][0]].push_back({tickets[i][1],i});
for(auto &[_,v]:g) sort(v.begin(),v.end());
dfs("JFK");
reverse(ans.begin(),ans.end());
return ans;
}
};