Difficulty Level : Easy
Submissions : 206
Asked In :
Marks :10
: 0 | : 0
There is a traveler who wants to travel to different places.
Given the edges between various cities, he wants to travel in a unique pattern.
Given a string $$$firstCity$$$, he decided to start from the $$$firstCity$$$, and visit its neighbor city which comes first in the list of edges. After visiting all the cities connected to a particular city, he will move to the next city in the list and repeat the same till all cities are visited.
You have to print the order of cities in which the traveler will travel.
The fist line contains an integer $$$n - (1 \le n \le 10^5)$$$, denoting the number of edges.
The next $$$n$$$ lines contain strings $$$city1$$$ and $$$city2$$$, denoting a directed edge from $$$city1$$$ to $$$city2$$$. $$$(1 \le |city1|,|city2| \le 20)$$$
The last line contains a string $$$firstCity$$$, denoting the starting city. $$$(1 \le |firstCity| \le 20)$$$
Print a single string in each line, denoting the order in which the traveler will travel.
5 Banglore Hyderabad Banglore Chennai Hyderabad Mumbai Hyderabad Delhi Chennai Kerala Banglore
Banglore Hyderabad Chennai Mumbai Delhi Kerala
In the sample case, the traveler will start from Bangalore, and first visits all its neighbors.
After Bangalore, he will visit Hyderabad, and then Chennai (as Hyderabad comes in the list earlier than Chennai). Then as he has visited Hyderabad earlier than Chennai, he will visit the neighbors of Hyderabad first, and then the neighbors of Chennai.
So the final ordering will be:
You need to login to view your submissions.
You need to login to view all submissions.
Result : Executed
Feel something is wrong with the test cases?
Result : Accepted
Test Cases :
But to Run or Submit the Problem, you need to Log In.
Continue to Log InYour challenge has been submitted successfully.
You will get a response soon via WhatsApp or Email.
Do let us know your issue.