CF1693B.Fake Plastic Trees
普及+/提高
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
We are given a rooted tree consisting of n vertices numbered from 1 to n. The root of the tree is the vertex 1 and the parent of the vertex v is pv.
There is a number written on each vertex, initially all numbers are equal to 0. Let's denote the number written on the vertex v as av.
For each v, we want av to be between lv and rv (lv≤av≤rv).
In a single operation we do the following:
- Choose some vertex v. Let b1,b2,…,bk be vertices on the path from the vertex 1 to vertex v (meaning b1=1, bk=v and bi=pbi+1).
- Choose a non-decreasing array c of length k of nonnegative integers: 0≤c1≤c2≤…≤ck.
- For each i (1≤i≤k), increase abi by ci.
What's the minimum number of operations needed to achieve our goal?
我们给出一棵有 n 个顶点的有根树,顶点编号为 1 到 n。树的根为顶点 1,顶点 v 的父节点为 pv。
每个顶点上写有一个数字,初始时所有数字均为 0。记顶点 v 上写的数字为 av。
对每个顶点 v,我们希望 av 满足 lv≤av≤rv。
一次操作定义如下:
- 任选一个顶点 v。设 b1,b2,…,bk 为从顶点 1 到顶点 v 的路径上的顶点(即 b1=1,bk=v,且对每个 i 有 bi=pbi+1);
- 任选一个长度为 k 的非负整数非递减数组 c:0≤c1≤c2≤…≤ck;
- 对每个 i(1≤i≤k),将 abi 增加 ci。
达成目标所需的最少操作次数是多少?
输入格式
The first line contains an integer t (1≤t≤1000) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer n (2≤n≤2⋅105) — the number of the vertices in the tree.
The second line of each test case contains n−1 integers, p2,p3,…,pn (1≤pi<i), where pi denotes the parent of the vertex i.
The i-th of the following n lines contains two integers li and ri (1≤li≤ri≤109).
It is guaranteed that the sum of n over all test cases doesn't exceed 2⋅105.
第一行包含一个整数 t(1≤t≤1000)——测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤2⋅105)——树中顶点的数量。
每个测试用例的第二行包含 n−1 个整数 p2,p3,…,pn(1≤pi<i),其中 pi 表示顶点 i 的父节点。
接下来的 n 行中,第 i 行包含两个整数 li 和 ri(1≤li≤ri≤109)。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
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=2 and c=[1,2], resulting in a1=1,a2=2.
In the second test case, we can achieve the goal with two operations: first, choose v=2 and c=[3,3], resulting in a1=3,a2=3,a3=0. Then, choose v=3,c=[2,7], resulting in a1=5,a2=3,a3=7.
在第一个测试用例中,我们可以通过一次操作达成目标:选择 v=2 和 c=[1,2],得到 a1=1,a2=2。
在第二个测试用例中,我们可以通过两次操作达成目标:首先,选择 v=2 和 c=[3,3],得到 a1=3,a2=3,a3=0;然后,选择 v=3 和 c=[2,7],得到 a1=5,a2=3,a3=7。
输入解题思路,AI测评打分。不知道怎么写?