Aho-Parasick

아직 제출이 없습니다시간 제한3초메모리 제한1024 MB

문제

We didn't come up with 5/10 or better joke, but some word mangling should suffice.

The Aho-Corasick algorithm takes a set of strings, which we will denote the dictionary as the input. Then it constructs the following structure:

There is a vertex corresponding to every prefix of one or more strings from the dictionary. There is also a vertex corresponding to the empty string. Two trees are formed from this set of vertices.

The first one is called the trie and it consists of all edges between nodes corresponding to strings ss and tt, such that tt can be obtained from ss by appending a single character.

The second one is called the suffix links and it consists of all edges between nodes corresponding to different strings ss and tt, such that ss is a suffix of tt and there is no string ww different from ss and tt with a corresponding trie node, such that ss is a suffix of ww and ww is a suffix of tt.

It can be shown that both the trie and the suffix links are trees. In this problem all edges are undirected.

In this problem trees are treated as sets of edges, and edges are treated as unordered pairs of vertices.

You are given two trees AA and BB on the same set of vertices SS. Construct a dictionary, such that

  1. Applying the Aho-Corasick algorithm to this dictionary yields trees isomorphic to the given ones. In other words, let VV denote the set of nodes (vertices) of the constructed trie and suffix links, TT --- the constructed trie and LL --- the suffix links. There must exists a bijection f:SVf:S \to V, such that _c1,c2Sc1,c2A    f(c1),f(c2)T,c1,c2B    f(c1),f(c2)L\forall\_{c1, c2 \in S} \\{c1,c2\\} \in A \iff \\{f(c1), f(c2)\\} \in T, \\{c1, c2\\} \in B \iff \\{f(c1), f(c2)\\} \in L.
  2. Total length of all strings in the dictionary doesn't exceed 31053 \cdot 10^5.

The alphabet size is limited by the total length of all strings. The answer is guaranteed to exist.

Note that there are pairs of trees such that it is possible to construct a dictionary which will satisfy the first requirement, but it is not possible to satisfy both. Such test cases are not present in this problem, however, because the answer is guaranteed to exist. In other words, the second requirement is meaningful.

입력

The first line contains a single integer nn (2n1052 \leq n \leq 10^5), the number of vertices.

Next n1n - 1 lines describe the trie. ii-th of them contains two integers aa and bb, meaning that there is an edge between vertices aa and bb (1a,bn1 \leq a, b \leq n).

Next n1n - 1 lines describe the suffix links in the same format.

It is guaranteed that both the trie and the suffix links are trees and an answer exists.

출력

The first line should contain a single integer kk (1k31051 \leq k \leq 3 \cdot 10^5) --- the number of the strings.

Next kk lines should contain the strings. Each should start with a single integer ll (1l31051 \leq l \leq 3 \cdot 10^5), length of the string. ll integers a_ia\_i (1a_i31051 \leq a\_i \leq 3 \cdot 10^5) representing the letters of the string should follow. Same a_ia\_i correspond to same letters and vice versa.

The total length of all strings should not exceed 31053 \cdot 10^5.