Reconstruct Traveler's Journey

Company: Paypal_25dec

Difficulty: medium

Problem Statement

Reconstruct Traveler's Journey A traveler visited a series of landmarks, one immediately after another, and visited each landmark exactly once. The travel journal is damaged, so the order of the visits is lost. What survives is a set of photos. Each photo shows exactly two landmarks that were visited one immediately after the other, and a photo does not record which of its two landmarks came first. Every consecutive pair of the journey appears in exactly one photo, and every photo shows one such consecutive pair. Reconstruct the journey. A journey and the same journey read backwards produce the same set of photos, so the direction is pinned by the input. Call a landmark an END landmark if it appears in exactly one photo; there are always exactly two of them. Read the photos in input order, and within a photo read the first ID before the second ID. The printed journey starts with whichever END landmark is reached first in that reading order. Input Format Line 1 contains a single in