#C9751. Lexicographically Minimal Topological Path

    ID: 53879 Type: Default 1000ms 256MiB

Lexicographically Minimal Topological Path

Lexicographically Minimal Topological Path

You are given a list of routes. Each route is a string representing a sequence of destinations separated by the arrow symbol, for example, "A->B->C". Each route implies a set of ordering constraints: for instance, in "A->B->C", destination A must appear before B, and B must appear before C in the final path. The unique task is: find a permutation of all unique destinations that satisfies all of these ordering constraints, with the extra requirement that the path must start with the first destination of the first given route. If there are multiple valid paths, output the lexicographically smallest one (when viewed as a string with destinations separated by '->').

Input Format: The input begins with an integer n on the first line, representing the number of routes. This is followed by n lines, each containing a route of the form X->Y->....

Note on constraints: It is guaranteed that at least one valid route exists that satisfies all ordering constraints and that the first destination of the first route has no prerequisites. All destinations are represented by uppercase letters.

inputFormat

The first line contains an integer n denoting the number of routes. Each of the following n lines contains a string representing a route in the form "A->B->C".

outputFormat

Output a single line containing the lexicographically smallest valid route (with destinations separated by '->') that visits all unique destinations while satisfying the ordering constraints and starting with the first destination of the first route.## sample

3
A->B
B->C
A->C->D
A->B->C->D

</p>