#C6788. Exploring Tunnels with Depth First Search

    ID: 50586 Type: Default 1000ms 256MiB

Exploring Tunnels with Depth First Search

In this problem, you are given a map of tunnels represented as a graph. Each node represents a junction and each tunnel connects two junctions. Starting from the first junction given in the input, you are required to perform a depth-first search (DFS). When exploring the neighbors of a junction, visit them in lexicographical order. The DFS algorithm can be summarized as follows:

( DFS(u): ) Mark node ( u ) as visited. For each neighbor ( v ) of ( u ) in sorted order, if ( v ) is not visited, recursively call ( DFS(v) ).

Output the order in which the nodes are visited (only those reachable from the starting node).

inputFormat

The input begins with an integer ( N ) indicating the number of nodes. Each of the next ( N ) lines describes one node. Each line starts with a node label (a string without spaces), followed by an integer ( K ) representing the number of neighbors, and then ( K ) space-separated neighbor labels.

outputFormat

Output a single line containing the nodes visited in the DFS order, separated by a single space.## sample

6
A 2 B C
B 3 A D E
C 2 A F
D 1 B
E 2 B F
F 2 C E
A B D E F C