At first sight, it seems that you are trying to implement the editorial’s approach, which is a good thing to do, so you can learn and improve, and apparentely, it’s not something the majority of people does, so, kudos for that…
Regarding the code itself, I would say that your nested for loops can be a possible cause for TLE…
I have to say that I still need to work around the graph solution modellation a bit better to fully understand it, but I would suggest you to use a custom data structure that does the same job as map and/or set difference, so you can easily retrieve all the cities once you have the 1st one determined…
Key idea I used:
Construct 2 arrays (one with the 1st cities and another with the second cities);
Eliminate all common entries on both arrays and retrieve the entry from the first cities array: this will be the 1st city;
Next use a mapping to connect cities, like:
map[city1] = city2; (this can be done while processing input)