Codeforces Round 240 (Div. 1) |
---|
Finished |
After a lot of trying, Mashmokh designed a problem and it's your job to solve it.
You have a tree T with n vertices. Each vertex has a unique index from 1 to n. The root of T has index 1. For each vertex of this tree v, you are given a list of its children in a specific order. You must perform three types of query on this tree:
The pseudo-code of function dfs(v):
// ls[v]: list of children of vertex v
// its i-th element is ls[v][i]
// its size is size(ls[v])
sequence result = empty sequence;
void dfs(vertex now)
{
add now to end of result;
for(int i = 1; i <= size(ls[v]); i = i + 1) //loop from i = 1 to i = size(ls[v])
dfs(ls[v][i]);
}
The first line of input contains two space-separated integers n, m (2 ≤ n ≤ 105; 1 ≤ m ≤ 105), the number of vertices of T and number of queries to perform.
The i-th of the following n lines contains an integer li (0 ≤ li ≤ n), number of i-th vertex's children. Then li space-separated integers follow, the j-th of them is the index of j-th child of i-th vertex. Note that the order of these vertices is important.
Each of the following m lines has one of the following format: "1 v u", "2 v h", or "3 k". The first number in the line is the type of query to perform according to the problem statement. The next numbers are description of the query.
It's guaranteed that all the queries are correct. For example, in the second-type query h is at least 2 and at most distance of v from root. Also in the third-type query there is at least one vertex with distance k from the root at the time the query is given.
For each query of the first or third type output one line containing the result of the query.
4 9
1 2
1 3
1 4
0
1 1 4
2 4 2
1 3 4
3 1
3 2
2 3 2
1 1 2
3 1
3 2
3
2
2
4
1
3
4
2 2
1 2
0
1 2 1
3 1
1
2
Name |
---|