CF414E.Mashmokh's Designed Problem

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

After a lot of trying, Mashmokh designed a problem and it's your job to solve it.

You have a tree TT with nn vertices. Each vertex has a unique index from 1 to nn . The root of TT has index 11 . For each vertex of this tree vv , 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 uu and vv ;
  2. given vv and hh , disconnect vv from its father and connect it to its hh -th ancestor; more formally, let's denote the path from vv to the root by x_{1},x_{2},...,x_{l} (h<l) , so that x1=vx_{1}=v and xlx_{l} is root; disconnect vv from its father ( x2x_{2} ) and connect it to xh+1x_{h+1} ; vertex vv must be added to the end of the child-list of vertex xh+1x_{h+1} ;
  3. in the vertex sequence produced by calling function dfs(root) find the latest vertex that has distance kk from the root.

The pseudo-code of function dfs(v):

<br></br>// ls[v]: list of children of vertex v <br></br>// its i-th element is ls[v][i]<br></br>// its size is size(ls[v])<br></br>sequence result = empty sequence;<br></br>void dfs(vertex now)<br></br>{<br></br> add now to end of result;<br></br> for(int i = 1; i <= size(ls[v]); i = i + 1) //loop from i = 1 to i = size(ls[v])<br></br> dfs(ls[v][i]);<br></br>}<br></br>

输入格式

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

The ii -th of the following nn lines contains an integer li (0<=li<=n)l_{i} (0<=l_{i}<=n) , number of ii -th vertex's children. Then lil_{i} space-separated integers follow, the jj -th of them is the index of jj -th child of ii -th vertex. Note that the order of these vertices is important.

Each of the following mm lines has one of the following format: " 11 vv uu ", " 22 vv hh ", or " 33 kk ". 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 hh is at least 2 and at most distance of vv from root. Also in the third-type query there is at least one vertex with distance kk 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.

输入输出样例

  • 输入#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
    

    输出#1

    3
    2
    2
    4
    1
    3
    4
    
  • 输入#2

    2 2
    1 2
    0
    1 2 1
    3 1
    

    输出#2

    1
    2
    
首页