CF1693B.Fake Plastic Trees

普及/提高-

通过率:0%

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 (lvavrv)(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: 0c1c2ck0 \leq c_1 \leq c_2 \leq \ldots \leq c_k .
  • For each ii (1ik)(1 \leq i \leq k) , increase abia_{b_i} by cic_i .

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

输入格式

The first line contains an integer tt (1t1000)(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 (2n2105)(2\le n\le 2 \cdot 10^5) — the number of the vertices in the tree.

The second line of each test case contains n1n - 1 integers, p2,p3,,pnp_2, p_3, \ldots, p_n (1pi<i)(1 \leq p_i < 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 (1liri109)(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 21052 \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 .

首页