A15367 | Paths on the Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 score array $s_1, s_2, \ldots, s_n$ .
A multiset of $k$ simple paths is called valid if the following two conditions are both true.
- Each path starts from $1$ .
- Let $c_i$ be the number of paths covering vertex $i$ . For each pair of vertices $(u,v)$ ( $2\le u,v\le n$ ) that have the same parent, $|c_u-c_v|\le 1$ holds.
The value of the path multiset is defined as $\sum\limits_{i=1}^n c_i s_i$ .It can be shown that it is always possible to find at least one valid multiset. Find the maximum value among all valid multisets.
A multiset of $k$ simple paths is called valid if the following two conditions are both true.
- Each path starts from $1$ .
- Let $c_i$ be the number of paths covering vertex $i$ . For each pair of vertices $(u,v)$ ( $2\le u,v\le n$ ) that have the same parent, $|c_u-c_v|\le 1$ holds.
The value of the path multiset is defined as $\sum\limits_{i=1}^n c_i s_i$ .It can be shown that it is always possible to find at least one valid multiset. Find the maximum value among all valid multisets.
输入格式
Each test contains multiple test cases. The first line contains a single integer $t$ ( $1 \leq t \leq 10^4$ ) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two space-separated integers $n$ ( $2 \le n \le 2 \cdot 10^5$ ) and $k$ ( $1 \le k \le 10^9$ ) — the size of the tree and the required number of paths.
The second line contains $n - 1$ space-separated integers $p_2,p_3,\ldots,p_n$ ( $1\le p_i\le n$ ), where $p_i$ is the parent of the $i$ -th vertex. It is guaranteed that this value describe a valid tree with root $1$ .
The third line contains $n$ space-separated integers $s_1,s_2,\ldots,s_n$ ( $0 \le s_i \le 10^4$ ) — the scores of the vertices.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$ .
The first line of each test case contains two space-separated integers $n$ ( $2 \le n \le 2 \cdot 10^5$ ) and $k$ ( $1 \le k \le 10^9$ ) — the size of the tree and the required number of paths.
The second line contains $n - 1$ space-separated integers $p_2,p_3,\ldots,p_n$ ( $1\le p_i\le n$ ), where $p_i$ is the parent of the $i$ -th vertex. It is guaranteed that this value describe a valid tree with root $1$ .
The third line contains $n$ space-separated integers $s_1,s_2,\ldots,s_n$ ( $0 \le s_i \le 10^4$ ) — the scores of the vertices.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$ .
输出格式
For each test case, print a single integer — the maximum value of a path multiset.
输入输出样例
输入 #1
2 5 4 1 2 1 3 6 2 1 5 7 5 3 1 2 1 3 6 6 1 4 10
输出 #1
54 56
In the first test case, one of optimal solutions is four paths $1 \to 2 \to 3 \to 5$ , $1 \to 2 \to 3 \to 5$ , $1 \to 4$ , $1 \to 4$ , here $c=[4,2,2,2,2]$ . The value equals to $4\cdot 6+ 2\cdot 2+2\cdot 1+2\cdot 5+2\cdot 7=54$ .
In the second test case, one of optimal solution is three paths $1 \to 2 \to 3 \to 5$ , $1 \to 2 \to 3 \to 5$ , $1 \to 4$ , here $c=[3,2,2,1,2]$ . The value equals to $3\cdot 6+ 2\cdot 6+2\cdot 1+1\cdot 4+2\cdot 10=56$ .
In the second test case, one of optimal solution is three paths $1 \to 2 \to 3 \to 5$ , $1 \to 2 \to 3 \to 5$ , $1 \to 4$ , here $c=[3,2,2,1,2]$ . The value equals to $3\cdot 6+ 2\cdot 6+2\cdot 1+1\cdot 4+2\cdot 10=56$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted