302174: CF414E. Mashmokh's Designed Problem

Memory Limit:512 MB Time Limit:4 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Mashmokh's Designed Problem

题意翻译

给定一棵 $n$ 个节点的有根树,每个点连出的边都有序,共有 $m$ 个操作。($n \le 10^5,m \le 10^5$) 操作有: - 1.查询两个点 $u,v$ 的距离 - 2.以 $v$ 为根的子树从树中分开,并添加一条与其第 $h$ 个祖先的连边作为该祖先的最后一个儿子。 - 3.查询从一个点出发,按边的顺序进行 dfs,深度为 $k$ 的最后遍历的点 **输入格式:** 第一行两个整数n和m 下面$n$行包含一个整数$l_i$($0$ <= $l_i$ <= $n$),描述了第$i$个数的第$j$个儿子,注意这些顶点的顺序。 接下来$m$行,每行如 “$\text{1 v u}$”,“$\text{2 v h}$”,或“$\text{3 k}$”。 **输出格式:** 对于每个1和3操作输出一行对应询问的结果

题目描述

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&lt;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): `<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<=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.

输出格式


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

Input

题意翻译

给定一棵 $n$ 个节点的有根树,每个点连出的边都有序,共有 $m$ 个操作。($n \le 10^5,m \le 10^5$) 操作有: - 1.查询两个点 $u,v$ 的距离 - 2.以 $v$ 为根的子树从树中分开,并添加一条与其第 $h$ 个祖先的连边作为该祖先的最后一个儿子。 - 3.查询从一个点出发,按边的顺序进行 dfs,深度为 $k$ 的最后遍历的点 **输入格式:** 第一行两个整数n和m 下面$n$行包含一个整数$l_i$($0$ <= $l_i$ <= $n$),描述了第$i$个数的第$j$个儿子,注意这些顶点的顺序。 接下来$m$行,每行如 “$\text{1 v u}$”,“$\text{2 v h}$”,或“$\text{3 k}$”。 **输出格式:** 对于每个1和3操作输出一行对应询问的结果

加入题单

上一题 下一题 算法标签: