AtCoder Beginner Contest 295
G
Minimum Reachable City
AtCoder
⌀ 2091
原题
← Ex→
翻译中…

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.

翻译中…

样例

Input #1
5
1 2 3 3
5
2 4
1 4 2
2 4
1 5 1
2 4
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$, $4$, and $5$ 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$.
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
Output #2
5
2
1
1
1
题目信息
题号
abc295_g
时间限制
3 s
内存限制
1024 MB

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