Problem: You are planning an around-the-world trip with your two best friends for the summer. There is a total of n cities that the three of you want to visit. As you are traveling around the world, you are worried about time zones and airport access. Therefore, some cities can only be visited after visiting another city first, which is in a nearby timezone or has an airport, which are expressed as a list of pairs (cityX,cityY) (cityX can only be visited after visiting cityY). Given the total number of cities and a list of dependency pairs, is it possible for you all to visit all cities? Your task is to write the function can_visit_all_cities, which determines whether visiting the n cities is possible or not given the dependencies. Requirements • Must run in O(m+n), and cannot use built in Python set/dictionary
Asked
Active
Viewed 26 times
1 Answers
0
This sounds like a dependency-graph. I don't know if python has a built in datastructure for this.
If you were to implement one on your own you'd have to use lists/sets though.

Moritz Groß
- 1,352
- 12
- 30