102956: [Atcoder]ABC295 G - Minimum Reachable City

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:2 Solved:0

Description

Score : $600$ points

Problem Statement

We have a directed graph $G_S$ with $N$ vertices numbered $1$ to $N$. It has $N-1$ edges. The $i$-th edge $(1\leq i \leq N-1)$ goes from vertex $p_i\ (1\leq p_i \leq i)$ to vertex $i+1$.

We have another directed graph $G$ with $N$ vertices numbered $1$ to $N$. Initially, $G$ equals $G_S$. Process $Q$ queries on $G$ in the order they are given. There are two kinds of queries as follows.

  • 1 u v : Add an edge to $G$ that goes from vertex $u$ to vertex $v$. It is guaranteed that the following conditions are satisfied.
    • $u \neq v$.
    • On $G_S$, vertex $u$ is reachable from vertex $v$ via some edges.
  • 2 x : Print the smallest vertex number of a vertex reachable from vertex $x$ via some edges on $G$ (including vertex $x$).

Constraints

  • $2\leq N \leq 2\times 10^5$
  • $1\leq Q \leq 2\times 10^5$
  • $1\leq p_i\leq i$
  • For each query in the first format:
    • $1\leq u,v \leq N$.
    • $u \neq v$.
    • On $G_S$, vertex $u$ is reachable from vertex $v$ via some edges.
  • For each query in the second format, $1\leq x \leq N$.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

$N$
$p_1$ $p_2$ $\dots$ $p_{N-1}$
$Q$
$\mathrm{query}_1$
$\mathrm{query}_2$
$\vdots$
$\mathrm{query}_Q$

Here, $\mathrm{query}_i$ denotes the $i$-th query and is in one of the following formats:

$1$ $u$ $v$
$2$ $x$

Output

Print $q$ lines, where $q$ is the number of queries in the second format. The $i$-th line $(1\leq j \leq q)$ should contain the answer to the $j$-th query in the second format.


Sample Input 1

5
1 2 3 3
5
2 4
1 4 2
2 4
1 5 1
2 4

Sample Output 1

4
2
1
  • At the time of the first query, only vertex $4$ is reachable from vertex $4$ via some edges on $G$.
  • At the time of the third query, vertices $2$, $3$, and $4$ are reachable from vertex $4$ via some edges on $G$.
  • At the time of the fifth query, vertices $1$, $2$, $3$, $4$, and $5$ are reachable from vertex $4$ via some edges on $G$.

Sample Input 2

7
1 1 2 2 3 3
10
2 5
1 5 2
2 5
1 2 1
1 7 1
1 6 3
2 5
2 6
2 1
1 7 1

Sample Output 2

5
2
1
1
1

Input

题意翻译

给定一张点数为 $N$ 的有向图,初始 $p_i(1\leq p_i \leq i,1 \leq i < N)$ 连向 $i+1$。 $Q$ 次操作,有两种: - `1 u v`:$u$ 向 $v$ 连一条有向边,保证最开始时 $v$ 能到达 $u$,$u \ne v$。 - `2 x`:询问 $x$ 能到达的点中编号最小的点。

Output

分数:$600$分

问题描述

我们有一个带有$N$个顶点的有向图$G_S$,顶点编号为$1$到$N$。 它有$N-1$条边。第$i$条边($1\leq i \leq N-1$)从顶点$p_i\ (1\leq p_i \leq i)$到顶点$i+1$。

我们还有一个带有$N$个顶点的有向图$G$,顶点编号为$1$到$N$。 最初,$G$等于$G_S$。 按给定的顺序在$G$上处理$Q$个查询。有两种类型的查询如下。

  • 1 u v : 在$G$中添加一条从顶点$u$到顶点$v$的边。 保证满足以下条件。
    • $u \neq v$。
    • 在$G_S$中,可以通过一些边从顶点$v$到达顶点$u$。
  • 2 x : 输出可以通过一些边从顶点$x$到达的顶点的最小顶点编号(包括顶点$x$)。

约束条件

  • $2\leq N \leq 2\times 10^5$
  • $1\leq Q \leq 2\times 10^5$
  • $1\leq p_i\leq i$
  • 对于第一个格式中的每个查询:
    • $1\leq u,v \leq N$。
    • $u \neq v$。
    • 在$G_S$中,可以通过一些边从顶点$v$到达顶点$u$。
  • 对于第二个格式中的每个查询,$1\leq x \leq N$。
  • 输入中的所有值都是整数。

输入

输入从标准输入以以下格式给出:

$N$
$p_1$ $p_2$ $\dots$ $p_{N-1}$
$Q$
$\mathrm{query}_1$
$\mathrm{query}_2$
$\vdots$
$\mathrm{query}_Q$

其中,$\mathrm{query}_i$表示第$i$个查询,可以是以下格式之一:

$1$ $u$ $v$
$2$ $x$

输出

打印$q$行,其中$q$是第二个格式中查询的数量。 第$i$行($1\leq j \leq q$)应该包含第二个格式中第$j$个查询的答案。


样例输入1

5
1 2 3 3
5
2 4
1 4 2
2 4
1 5 1
2 4

样例输出1

4
2
1
  • 在第一次查询时,只有顶点$4$可以通过一些边从顶点$4$到达$G$。
  • 在第三次查询时,顶点$2$、$3$、$4$和$5$可以通过一些边从顶点$4$到达$G$。
  • 在第五次查询时,顶点$1$、$2$、$3$、$4$和$5$可以通过一些边从顶点$4$到达$G$。

样例输入2

7
1 1 2 2 3 3
10
2 5
1 5 2
2 5
1 2 1
1 7 1
1 6 3
2 5
2 6
2 1
1 7 1

样例输出2

5
2
1
1
1

加入题单

算法标签: