He does a poor job, though, and wants to learn from Bill’s routes. Unfortunately Bill has left only a few notes for his successor. Bill never wrote his routes completely, he only left lots of little sheets on which he had written two consecutive steps of the routes. […]
This made much sense, since one step always required something from the previous step. George however would like to have a route written down as one long sequence of all the steps in the correct order.
Problem: Sphere Online Judge (SPOJ) – Problem SCAVHUNT
Solution:
scenarios = int(raw_input()) for i in xrange(0, scenarios): S = int(raw_input()) myDict = dict() myRevDict = dict() for j in xrange(0, S - 1): fromLocation, toLocation = raw_input().split() myDict[fromLocation] = toLocation myRevDict[toLocation] = fromLocation # find start start = "" for key in myDict.keys(): if key not in myRevDict: start = key break # print message print "Scenario #%i:" % (i + 1) # print path currentKey = start for l in xrange(0, S - 1): print currentKey currentKey = myDict[currentKey] print currentKey print ""