#P10065. [SNOI2024] 字符树

[SNOI2024] 字符树

Problem Description

You are given a rooted tree with nn nodes, with node 11 as the root. Each edge has a character c{0,1}c \in \{0, 1\}. Let SuS_u be the string formed by writing down, in order, the characters on the path from the root to node uu. It is guaranteed that, for each node, the characters on the edges from this node to its children are pairwise distinct.

For each node uu, there is a value valu\mathit{val}_u and a limit aua_u. For a node uu, if a node vv satisfies that SuS_u is a suffix of SvS_v, then we call vv an extension node of uu.

Alice has a string SS. Initially, let S=SuS = S_u. Now she can delete some suffix characters so that SS becomes SS', and then tell SS' to Bob.

Bob receives a string SS'. He needs to append some characters after SS' to obtain SS''. For an extension node vv of some uu, if S=SvS'' = S_v and Sav\lvert S' \rvert \ge a_v, then Bob gains profit valv\mathit{val}_v. Of course, Bob can perform such an operation only once, so he will choose, among all valid vv, the one with the maximum valv\mathit{val}_v. If there is no valid vv, Bob can gain only 00.

Now Alice wants to know: among all S+1\lvert S \rvert + 1 deletion choices (deleting 0S0 \sim \lvert S \rvert characters), what is the sum of the profits Bob can obtain?

For each uu, you need to answer Alice’s query.

Formally:

For each node uu, compute $\mathit{ans}_u = \sum\limits_{0 \le i \le \lvert S_u \rvert} \max\limits_{i \ge a_v \land S_u = S_v[\lvert S_v \rvert - \lvert S_u \rvert + 1, \lvert S_v \rvert] \land S_u[1, i] = S_v[1, i]} \mathit{val}_v$。

In particular, if for some uu there is no vv satisfying the conditions, then max=0\max = 0.

Here S[l,r]S[l, r] denotes the substring consisting of the ll-th to the rr-th characters of string SS. In particular, S[x+1,x]S[x + 1, x] denotes the empty string. S\lvert S \rvert denotes the length of SS, and \land means “and”.

Input Format

Multiple test cases. The first line contains an integer TT, denoting the number of test cases.
For each test case, the first line contains a positive integer nn, denoting the number of nodes.
The next n1n - 1 lines each contain two integers fai,ci\mathit{fa}_i, c_i, denoting the parent index of node ii and the character on the edge.
The next line contains nn positive integers $\mathit{val}_1, \mathit{val}_2, \ldots, \mathit{val}_n$.
The next line contains nn non-negative integers a1,a2,,ana_1, a_2, \ldots, a_n.

Output Format

Output one line with nn integers $\mathit{ans}_1, \mathit{ans}_2, \ldots, \mathit{ans}_n$.

1
5
1 0
1 1
2 0
2 1
1 2 3 4 5
0 1 0 1 2

3 4 6 8 5

Hint

[Sample #1 Explanation]

The following table shows, for fixed uu and ii, the maximum value of valv\mathit{val}_v in the formula.

u=1u = 1 u=2u = 2 u=3u = 3 u=4u = 4 u=5u = 5
i=0i = 0 33 00 33 00 00
i=1i = 1 - 44 44
i=2i = 2 - 55

[Sample #2]

See the attachments tree/tree2.in and tree/tree2.ans.

This sample satisfies the constraints of test points 353 \sim 5.


[Sample #3]

See the attachments tree/tree3.in and tree/tree3.ans.

This sample satisfies the constraints of test points 9109 \sim 10.


[Sample #4]

See the attachments tree/tree4.in and tree/tree4.ans.

This sample satisfies the constraints of test points 111211 \sim 12.


[Constraints]

For all testdata, it is guaranteed that 1T51 \le T \le 5, 1n5×1051 \le n \le 5 \times {10}^5, 1vali1091 \le \mathit{val}_i \le {10}^9, 1fai<i1 \le \mathit{fa}_i < i, ci{0,1}c_i \in \{0, 1\}, and 0ain0 \le a_i \le n.

Specifically:

Test Point ID nn \le Special Property
121 \sim 2 100100 None
353 \sim 5 2×1032 \times {10}^3
686 \sim 8 104{10}^4
9109 \sim 10 105{10}^5 A
111211 \sim 12 B
131613 \sim 16 None
172017 \sim 20 5×1055 \times {10}^5

Special property A: ci=0c_i = 0.
Special property B: fai=i2\mathit{fa}_i = \lfloor \frac{i}{2} \rfloor.

Translated by ChatGPT 5