Codeforces Round 240 (Div. 1)
E
Mashmokh's Designed Problem
Codeforces
3200
原题
← D→
翻译中…

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:

翻译中…
  1. find distance (the number of edges in the shortest path) between $*u*$ and $*v*$;
  2. given $*v*$ and $*h*$, disconnect $*v*$ from its father and connect it to its $*h*$-th ancestor; more formally, let's denote the path from $*v*$ to the root by $*x*_{1}, *x*_{2}, ..., *x*_{*l*} (*h* < *l*)$, so that $*x*_{1} = *v*$ and $*x*_{*l*}$ is root; disconnect $*v*$ from its father ($*x*_{2}$) and connect it to $*x*_{*h* + 1}$; vertex $*v*$ must be added to the end of the child-list of vertex $*x*_{*h* + 1}$;
  3. in the vertex sequence produced by calling function dfs(root) find the latest vertex that has distance $*k*$ from the root.
翻译中…

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]);
}

翻译中…

Input

翻译中…

The first line of input contains two space-separated integers $*n*, *m* (2 ≤ *n* ≤ 10^{5}; 1 ≤ *m* ≤ 10^{5})$, the number of vertices of $*T*$ and number of queries to perform.

翻译中…

The $*i*$-th of the following $*n*$ lines contains an integer $*l*_{*i*} (0 ≤ *l*_{*i*} ≤ *n*)$, number of $*i*$-th vertex's children. Then $*l*_{*i*}$ 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.

翻译中…

Output

翻译中…

For each query of the first or third type output one line containing the result of the query.

翻译中…

样例

Input #1
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
Output #1
3
2
2
4
1
3
4
InputCopy OutputCopy
Input #2
2 2
1 2
0
1 2 1
3 1
Output #2
1
2
InputCopy OutputCopy
题目信息
题号
414E
时间限制
4 s
内存限制
512 MB
标签
数据结构

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。