CF2244F.Anya Loves Trees!

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Anya considers a rooted tree with the root at vertex 11. The leaves of the tree contain integers from 11 to kk, where kk is the number of leaves. Each leaf contains exactly one number. For every vertex, its children are ordered from left to right in increasing order of their indices.

Anya noticed that if we list the leaves from left to right, their values form a sequence. She wants this sequence to become strictly increasing.

To achieve this, Anya can perform the following operation: choose any vertex and cyclically shift its children to the left∗^{\text{∗}}. For example, if a vertex has children in the order [1,2,3][1, 2, 3], after the shift the order becomes [2,3,1][2, 3, 1]. This operation can be applied to any vertices any number of times.

A visual example of a cyclic shift of the children of vertex 11:

An example of a cyclic shift of children.

Help Yura determine whether Anya can make the sequence of integers in the leaves strictly increasing from left to right using such operations.

∗^{\text{∗}}A cyclic left shift is an operation where all elements are shifted left by 11, and the first element becomes the last.

安雅考虑一棵以顶点 11 为根的有根树。树的叶子节点中存放着从 11 到 kk 的整数,其中 kk 是叶子节点的总数。每个叶子节点恰好包含一个数字。对于任意一个顶点,其子节点按索引从小到大的顺序从左到右排列。

安雅注意到,如果从左到右依次列出所有叶子节点,它们的数值会构成一个序列。她希望该序列严格递增。

为实现这一目标,安雅可以执行如下操作:任选一个顶点,并将其子节点向左循环移位∗^{\text{∗}}。例如,若某顶点的子节点顺序为 [1,2,3][1, 2, 3],则移位后变为 [2,3,1][2, 3, 1]。该操作可对任意顶点执行任意多次。

对顶点 11 的子节点进行循环移位的图示示例:

子节点循环移位示例。

请帮助尤拉判断:安雅能否通过上述操作,使得从左到右的叶子节点所含整数序列严格递增?

∗^{\text{∗}}循环左移是指将所有元素向左移动一位,原第一个元素移至末尾的操作。

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the number of vertices.

The next line contains n−1n - 1 integers p2,p3,…,pnp_2, p_3, \dots, p_n (1≤pi≤n1 \le p_i \le n), where pip_i is the parent of vertex ii. The children of each vertex are ordered from left to right in increasing order of their indices.

The third line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (0≤ai≤n0 \le a_i \le n). If vertex ii is not a leaf, then ai=0a_i = 0. If vertex ii is a leaf, then ai>0a_i \gt 0 and represents the number written in that vertex. It is guaranteed that all positive values aia_i form a permutation.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 顶点的数量。

下一行包含 n−1n - 1 个整数 p2,p3,…,pnp_2, p_3, \dots, p_n(1≤pi≤n1 \le p_i \le n),其中 pip_i 表示顶点 ii 的父节点。每个顶点的子节点按其索引从小到大的顺序从左到右排列。

第三行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(0≤ai≤n0 \le a_i \le n)。若顶点 ii 不是叶子节点,则 ai=0a_i = 0;若顶点 ii 是叶子节点,则 ai>0a_i \gt 0,且表示写在该顶点上的数字。保证所有正数 aia_i 构成一个排列。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output 'YES" if it is possible to reorder the integers in the leaves to be strictly increasing, and 'NO" otherwise.

You may output each letter in any case (lowercase or uppercase). For example, the strings 'yEs", 'yes", 'Yes", and 'YES" will be accepted.

对于每个测试用例,如果可以重新排列叶子节点中的整数,使其严格递增,则输出 YES;否则输出 NO。

你可以以任意大小写形式输出每个字母(小写或大写)。例如,字符串 yEs、yes、Yes 和 YES 均可被接受。

输入输出样例

  • 输入#1

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

    输出#1

    YES
    YES
    YES
    NO

说明/提示

null

输入解题思路,AI测评打分。不知道怎么写?

首页