This page is still under construction.

Parts of this page are still being built. What you see may change.

Torn to Pieces

Interview

Time limit2sMemory limit256 MB

Summary
Rebuild the subway graph from the torn map pieces and print the stations on the path from the start to the destination, or no route found.
Level

Medium4 of 10

Topics
Graph, BFS
Solved
No attempts yet

Problem

You have reached the big city, but your trip is not over. You still have to ride the subway to your final destination. The information booth in the station is empty and the maps are all gone. On the floor you see torn pieces of a subway map. Can you put enough of them together to work out how to reach your destination?

Each piece holds exactly one station and lists the names of every station directly connected to it. A connection between two stations is bidirectional, so you may travel it in either direction. Using all of the pieces you found, work out the order of the stations you pass through on the way from the starting station to the destination. If the pieces are not enough to determine a route, report that no route exists.

Input

The first line contains the number of map pieces NN that were found. (2≤N≤322 \le N \le 32)

Each of the next NN lines describes one station drawn on a piece. The line starts with the name of that station, followed by the space separated names of the stations directly connected to it. There are at most N−1N-1 connected stations.

The last line contains the name of the starting station and the name of the destination station. The destination station is different from the starting station.

A station name is a string of at most 20 letters a to z and A to Z, and letter case matters. There is at most one route from the starting station to the destination that visits no station twice.

Output

Print the names of the stations along the way from the starting station to the destination, in order, separated by single spaces on one line. The starting station and the destination station are included. If the pieces are not enough to find a route, print no route found.

Examples3

  1. Example 1

    Input
    3
    Uptown Midtown
    Midtown Uptown Downtown
    Downtown Midtown
    Uptown Downtown
    
    Expected output
    Uptown Midtown Downtown
    
  2. Example 2

    Input
    6
    A B
    B A D
    C D
    E D F G
    F E
    G E
    F A
    
    Expected output
    F E D B A
    
  3. Example 3

    Input
    4
    FirstStop SecondStop
    SecondStop FirstStop ThirdStop
    FifthStop FourthStop SixthStop
    SixthStop FifthStop
    FirstStop FifthStop
    
    Expected output
    no route found