EPIC Institute of Technology Round August 2024 (Div. 1 + Div. 2)
D2
DFS Checker (Hard Version)
Codeforces
2300
原题
← D1E →
翻译中…

This is the hard version of the problem. In this version, you are given a generic tree and the constraints on $n$ and $q$ are higher. You can make hacks only if both versions of the problem are solved.

翻译中…

You are given a rooted tree consisting of $n$ vertices. The vertices are numbered from $1$ to $n$, and the root is the vertex $1$. You are also given a permutation $p_1, p_2, \ldots, p_n$ of $[1,2,\ldots,n]$.

翻译中…

You need to answer $q$ queries. For each query, you are given two integers $x$, $y$; you need to swap $p_x$ and $p_y$ and determine if $p_1, p_2, \ldots, p_n$ is a valid DFS order$^\dagger$ of the given tree.

翻译中…

Please note that the swaps are persistent through queries.

翻译中…

$^\dagger$ A DFS order is found by calling the following $\texttt{dfs}$ function on the given tree.

翻译中…

dfs_order = []

function dfs(v):
    append v to the back of dfs_order
    pick an arbitrary permutation s of children of v
    for child in s:
        dfs(child)
dfs(1)

翻译中…

Note that the DFS order is not unique.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$, $q$ ($2\le n\le 3\cdot 10^5$, $2\le q\le 10^5$) — the number of vertices in the tree and the number of queries.

翻译中…

The next line contains $n-1$ integers $a_2,a_3,\ldots,a_n$ ($1\le a_i \lt i$) — the parent of each vertex in the given tree.

翻译中…

The next line contains $n$ integers $p_1,p_2,\ldots,p_n$ ($1\le p_i\le n$, all $p_i$ are distinct) — the initial permutation $p$.

翻译中…

The next $q$ lines each contain two integers $x$, $y$ ($1\le x,y\le n,x\neq y$) — the positions of the elements to swap in the permutation.

翻译中…

It is guaranteed that the sum of all $n$ does not exceed $3\cdot 10^5$, and the sum of all $q$ does not exceed $10^5$.

翻译中…

Output

翻译中…

For each test case, print $q$ lines corresponding to the $q$ queries. For each query, output $\texttt{YES}$ if there is a DFS order that exactly equals the current permutation, and output $\texttt{NO}$ otherwise.

翻译中…

You can output $\texttt{Yes}$ and $\texttt{No}$ in any case (for example, strings $\texttt{yEs}$, $\texttt{yes}$, $\texttt{Yes}$, and $\texttt{YES}$ will be recognized as a positive response).

翻译中…

Note

翻译中…

In the first test case, the permutation $p_1, p_2, \ldots, p_n$ after each modification is $[1,3,2],[1,2,3],[3,2,1]$, respectively. The first two permutations are valid DFS orders; the third is not a DFS order.

翻译中…

In the second test case, the permutation $p_1, p_2, \ldots, p_n$ after each modification is $[1,2,5,4,3,6,7],[1,3,5,4,2,6,7],[1,3,7,4,2,6,5],[1,3,7,6,2,4,5]$, respectively.

翻译中…

样例

Input #1
3
3 3
1 1
1 2 3
2 3
3 2
1 3
7 4
1 1 2 2 3 3
1 2 3 4 5 6 7
3 5
2 5
3 7
4 6
5 4
1 1 3 4
2 3 4 5 1
5 1
4 5
3 4
2 3
Output #1
YES
YES
NO
YES
NO
NO
YES
YES
NO
NO
YES
InputCopy OutputCopy
题目信息
题号
2002D2
时间限制
2 s
内存限制
512 MB
标签
二分
数据结构
DFS 及类似
图论
哈希
树

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