CF1693B.Fake Plastic Trees

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

We are given a rooted tree consisting of nn vertices numbered from 11 to nn. The root of the tree is the vertex 11 and the parent of the vertex vv is pvp_v.

There is a number written on each vertex, initially all numbers are equal to 00. Let's denote the number written on the vertex vv as ava_v.

For each vv, we want ava_v to be between lvl_v and rvr_v (lv≤av≤rv)(l_v \leq a_v \leq r_v).

In a single operation we do the following:

  • Choose some vertex vv. Let b1,b2,…,bkb_1, b_2, \ldots, b_k be vertices on the path from the vertex 11 to vertex vv (meaning b1=1b_1 = 1, bk=vb_k = v and bi=pbi+1b_i = p_{b_{i + 1}}).
  • Choose a non-decreasing array cc of length kk of nonnegative integers: 0≤c1≤c2≤…≤ck0 \leq c_1 \leq c_2 \leq \ldots \leq c_k.
  • For each ii (1≤i≤k)(1 \leq i \leq k), increase abia_{b_i} by cic_i.

What's the minimum number of operations needed to achieve our goal?

我们给出一棵有 nn 个顶点的有根树,顶点编号为 11 到 nn。树的根为顶点 11,顶点 vv 的父节点为 pvp_v。

每个顶点上写有一个数字,初始时所有数字均为 00。记顶点 vv 上写的数字为 ava_v。

对每个顶点 vv,我们希望 ava_v 满足 lv≤av≤rvl_v \leq a_v \leq r_v。

一次操作定义如下:

  • 任选一个顶点 vv。设 b1,b2,…,bkb_1, b_2, \ldots, b_k 为从顶点 11 到顶点 vv 的路径上的顶点(即 b1=1b_1 = 1,bk=vb_k = v,且对每个 ii 有 bi=pbi+1b_i = p_{b_{i + 1}});
  • 任选一个长度为 kk 的非负整数非递减数组 cc:0≤c1≤c2≤…≤ck0 \leq c_1 \leq c_2 \leq \ldots \leq c_k;
  • 对每个 ii(1≤i≤k1 \leq i \leq k),将 abia_{b_i} 增加 cic_i。

达成目标所需的最少操作次数是多少?

输入格式

The first line contains an integer tt (1≤t≤1000)(1\le t\le 1000) — the number of test cases. The description of the test cases follows.

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

The second line of each test case contains n−1n - 1 integers, p2,p3,…,pnp_2, p_3, \ldots, p_n (1≤pi<i)(1 \leq p_i \lt i), where pip_i denotes the parent of the vertex ii.

The ii-th of the following nn lines contains two integers lil_i and rir_i (1≤li≤ri≤109)(1 \le l_i \le r_i \le 10^9).

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

第一行包含一个整数 tt(1≤t≤10001\le t\le 1000)——测试用例的数量。随后是各测试用例的描述。

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

每个测试用例的第二行包含 n−1n - 1 个整数 p2,p3,…,pnp_2, p_3, \ldots, p_n(1≤pi<i1 \leq p_i \lt i),其中 pip_i 表示顶点 ii 的父节点。

接下来的 nn 行中,第 ii 行包含两个整数 lil_i 和 rir_i(1≤li≤ri≤1091 \le l_i \le r_i \le 10^9)。

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

输出格式

For each test case output the minimum number of operations needed.

对于每个测试用例,输出所需的最少操作次数。

输入输出样例

  • 输入#1

    4
    2
    1
    1 5
    2 9
    3
    1 1
    4 5
    2 4
    6 10
    4
    1 2 1
    6 9
    5 6
    4 5
    2 4
    5
    1 2 3 4
    5 5
    4 4
    3 3
    2 2
    1 1

    输出#1

    1
    2
    2
    5

说明/提示

In the first test case, we can achieve the goal with a single operation: choose v=2v = 2 and c=[1,2]c = [1, 2], resulting in a1=1,a2=2a_1 = 1, a_2 = 2.

In the second test case, we can achieve the goal with two operations: first, choose v=2v = 2 and c=[3,3]c = [3, 3], resulting in a1=3,a2=3,a3=0a_1 = 3, a_2 = 3, a_3 = 0. Then, choose v=3,c=[2,7]v = 3, c = [2, 7], resulting in a1=5,a2=3,a3=7a_1 = 5, a_2 = 3, a_3 = 7.

在第一个测试用例中,我们可以通过一次操作达成目标:选择 v=2v = 2 和 c=[1,2]c = [1, 2],得到 a1=1,a2=2a_1 = 1, a_2 = 2。

在第二个测试用例中,我们可以通过两次操作达成目标:首先,选择 v=2v = 2 和 c=[3,3]c = [3, 3],得到 a1=3,a2=3,a3=0a_1 = 3, a_2 = 3, a_3 = 0;然后,选择 v=3v = 3 和 c=[2,7]c = [2, 7],得到 a1=5,a2=3,a3=7a_1 = 5, a_2 = 3, a_3 = 7。

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

首页