#P6999. [NEERC 2013] Dictionary
[NEERC 2013] Dictionary
题目描述
Petr and Dmitry are working on a novel data compression scheme. Their task is to compress a given set of words. To compress a given set of words they have to build a rooted tree. Each edge of the tree is marked with exactly one letter.
Let us define a dictionary that is produced by this kind of tree as a set of words that can be constructed by concatenating letters on edges on any path from any vertex in the tree (not necessarily root) and going away from root down to the leaves (but not necessarily finishing on a leaf).
Boys have to construct such a tree with a dictionary that is a superset of the set of words that they are given to compress. This tree should have the smallest number of vertices between trees that satisfy the above condition. Any tree with the same number of vertices will do. Your task is to help them.

For example, in a tree on the picture above with the root marked as , a path from to reads north, a path from to reads eastern, a path from to reads european, a path from to reads regional, and a path from to reads contest.
输入格式
The first line of the input file contains the number of words in a given set . The following lines contain different non-empty words, one word per line, consisting of lowercase English letters. The length of each word is at most characters.
输出格式
On the first line output the number of vertices in the tree . The following lines shall contain descriptions of tree vertices, one description per line. Vertices are indexed from to in the order of their corresponding description lines. If the corresponding vertex is a tree root, then its description line shall contain a single integer number , otherwise its description line shall contain an index of its parent node and a letter on the edge to its parent node, separated by a space.
5
north
eastern
european
regional
contest
31
0
7 n
2 o
18 t
4 h
29 e
17 a
7 s
8 t
9 e
10 r
11 n
6 u
13 r
14 o
15 p
16 e
3 r
18 e
19 g
20 i
21 o
22 n
23 a
24 l
1 c
26 o
27 n
28 t
6 s
30 t
提示
Time limit: 1 s, Memory limit: 128 MB.