QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 512 MB Total points: 100

#7792. Tree Topological Order Counting

Statistics

给定一颗 $ n $ 个点的有根树,$ 1 $ 是根,记 $ u $ 的父亲是 $ fa_u $。另给出一长度为 $ n $ 的权值序列 $ b $。

称一个长度为 $ n $ 的排列 $ a $ 为这颗树的合法拓扑序,当且仅当 $ \forall 2 \le u \le n,a_u > a_{fa_u} $。

对每个点 $ u $,定义 $ f(u) $ 为,在所有这颗树的合法拓扑序中,$ b_{a_u} $ 之和。

现在对 $ 1 \le u \le n $,求 $ f(u) \bmod 10^9+7 $。

输入格式

第一行一个整数 $ n $ 表示树的点数。

第二行 $ n-1 $ 个整数,第 $ i $ 个表示 $ fa_{i+1} $,描述树的结构。

第三行 $ n $ 个整数,第 $ i $ 个表示 $ b_i $,描述权值序列。

输出格式

一行 $ n $ 个整数,第 $ i $ 个表示 $ f(i) \bmod 10^9+7 $。

样例

样例输入 1

5
1 1 3 2 
3 5 4 4 1 

样例输出 1

18 27 27 15 15 

样例输入 2

5
1 1 3 1
1 2 3 4 5

样例输出 2

12 42 32 52 42

数据范围

Subtask$ n \le $特殊限制分值
$ 1 $$ 10 $$ 5 $
$ 2 $$ 20 $$ 10 $
$ 3 $$ 100 $$ 15 $
$ 4 $$ 350 $$ 15 $
$ 5 $$ 3000 $A$ 15 $
$ 6 $$ 3000 $$ 20 $
$ 7 $$ 5000 $$ 20 $

特殊限制 A:$ \forall 1 \le i \le n,b_i=i $。

对于所有数据:$ 2 \le n \le 5000 $,$ 1 \le fa_i < i $,$ 0 \le b_i < 10^9+7 $。

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.