Beautiful Automata
Time limit2sMemory limit512 MB
Given a DAG, find the lexicographically smallest string whose suffix automaton is exactly this graph, or report -1 if none exists.
- Level
Hard10 of 10
- Topics
- Graph, Topological sort, String, Greedy
- Solved
- No attempts yet
Statement
Oleksandr is a fan of strings. His favourite data structure is the suffix automaton.
Consider a string of lowercase English letters. The suffix automaton of is the smallest directed acyclic graph (the one with the fewest vertices) with a letter written on each edge and a fixed starting vertex , such that
Oleksandr likes suffix automata more than any other graphs. He calls a directed acyclic graph -beautiful if a lowercase letter can be written on each edge and a starting vertex chosen so that becomes the suffix automaton of .
Oleksandr likes lexicographically small strings. Given a graph , find the lexicographically smallest string such that is -beautiful.
Input
The first line contains two integers and (, ), the number of vertices and the number of edges in . Each of the next lines contains two integers and (, ), denoting a directed edge from to . It is guaranteed that is acyclic.
Output
Output a single line containing the lexicographically smallest string consisting of lowercase English letters such that is -beautiful. If no such string exists, output .
Hint
The suffix automata for the first three sample tests are shown below.
