QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 512 MB Total points: 100 Hackable ✓

#13618. 三角形

统计

Snuke 有一棵 $n$ 个点的有根树,每个点有权值 $w_i$,初始每个结点上都没有石子。

Snuke 准备了一些石子,并把它们拿在手中。她可以进行以下两种操作任意多次:

  1. 从手中取 $w_i$ 个石子放在结点 $i$ 上,进行该操作要求结点 $i$ 的所有孩子 $j$ 上都有 $w_j$ 个石子。
  2. 将结点 $i$ 上的所有石子收回手中。

Takahashi 想知道对于每个 $i$,为了在结点 $i$ 上放 $w_i$ 个石子,Snuke 至少需要准备多少石子。

输入格式

从标准输入读入数据。

第一行一个数字 $T$ 表示这个子任务的编号。

第二行一个正整数 $n$ 。$(n \leq 2 \times 10^5)$

第三行 $n-1$ 个正整数,第 $i-1$ 个数 $p_i$ 表示 $i$ 的父亲。$(p_i < i)$

第四行 $n$ 个正整数,第 $i$ 个数为 $w_i$ 。

输出格式

输出到标准输出。

输出一行 $n$ 个正整数,第 $i$ 个数为结点 $i$ 的答案。

样例一

input

0
3
1 2
1 1 1

output

2 2 1

样例二

input

0
3
1 1
1 1 1

output

3 1 1

限制及约定

对于所有数据,保证:

  • $n \leq 2 \times 10^5$
  • $1 \le p_i < i$
  • $1 \le w_i \le 10^9$
子任务编号特殊性质分值
1$n \leq 15$9
2$n \leq 2000$19
3所有$w$相同6
4$w_i \leq w_{i+1}$12
5$n \leq 2000$ 且所有点度数 $\leq 2$5
6除根结点外所有点度数 $\leq 2$13
7无特殊限制36
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.