Confused about why my code does not work


  • 0
    Q
    class Solution {
    public:
        
        vector<string> findItinerary(vector<pair<string, string>> tickets) {
            map<string, multiset<string>> mp;
            for(auto it:tickets){
                string key = it.first, val = it.second;
                mp[key].insert(val);
            }
            vector<string> res;
    		res.push_back("JFK");
    		dfs("JFK", res, tickets.size()+ 1, mp);
    		return res;
        }
        
        bool dfs(string start, vector<string> &res, int n, map<string, multiset<string>> &mp){
            if(res.size() == n)
                return true;
            for(auto it = mp[start].begin();it!=mp[start].end();it++){
                string dest = *it;
                res.push_back(dest);
                mp[start].erase(it);
                if(dfs(dest,res,n,mp))
                    return true;
                res.pop_back();
                mp[start].insert(dest);
            }
                
            return false;
        }
    };
    

    It works fine on several cases, but got a

    "terminate called after throwing an instance of 'std::logic_error'
      what():  basic_string::_M_construct null not valid"
    

    when is input is

    [["EZE","TIA"],["EZE","HBA"],["AXA","TIA"],["JFK","AXA"],["ANU","JFK"],["ADL","ANU"],["TIA","AUA"],["ANU","AUA"],["ADL","EZE"],["ADL","EZE"],["EZE","ADL"],["AXA","EZE"],["AUA","AXA"],["JFK","AXA"],["AXA","AUA"],["AUA","ADL"],["ANU","EZE"],["TIA","ADL"],["EZE","ANU"],["AUA","ANU"]]
    

    Could somebody help me to find the bug of my code? Thank you


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.