#P10065. [SNOI2024] 字符树
[SNOI2024] 字符树
Problem Description
You are given a rooted tree with nodes, with node as the root. Each edge has a character . Let be the string formed by writing down, in order, the characters on the path from the root to node . It is guaranteed that, for each node, the characters on the edges from this node to its children are pairwise distinct.
For each node , there is a value and a limit . For a node , if a node satisfies that is a suffix of , then we call an extension node of .
Alice has a string . Initially, let . Now she can delete some suffix characters so that becomes , and then tell to Bob.
Bob receives a string . He needs to append some characters after to obtain . For an extension node of some , if and , then Bob gains profit . Of course, Bob can perform such an operation only once, so he will choose, among all valid , the one with the maximum . If there is no valid , Bob can gain only .
Now Alice wants to know: among all deletion choices (deleting characters), what is the sum of the profits Bob can obtain?
For each , you need to answer Alice’s query.
Formally:
For each node , 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 there is no satisfying the conditions, then .
Here denotes the substring consisting of the -th to the -th characters of string . In particular, denotes the empty string. denotes the length of , and means “and”.
Input Format
Multiple test cases. The first line contains an integer , denoting the number of test cases.
For each test case, the first line contains a positive integer , denoting the number of nodes.
The next lines each contain two integers , denoting the parent index of node and the character on the edge.
The next line contains positive integers $\mathit{val}_1, \mathit{val}_2, \ldots, \mathit{val}_n$.
The next line contains non-negative integers .
Output Format
Output one line with 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 and , the maximum value of in the formula.
| - | |||||
| - | |||||
[Sample #2]
See the attachments tree/tree2.in and tree/tree2.ans.
This sample satisfies the constraints of test points .
[Sample #3]
See the attachments tree/tree3.in and tree/tree3.ans.
This sample satisfies the constraints of test points .
[Sample #4]
See the attachments tree/tree4.in and tree/tree4.ans.
This sample satisfies the constraints of test points .
[Constraints]
For all testdata, it is guaranteed that , , , , , and .
Specifically:
| Test Point ID | Special Property | |
|---|---|---|
| None | ||
| A | ||
| B | ||
| None | ||
Special property A: .
Special property B: .
Translated by ChatGPT 5