#P10536. [Opoi 2024] 二十六点

    ID: 11597 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>线段树树上启发式合并O2优化树链剖分单调栈

[Opoi 2024] 二十六点

Background

Twenty-six points:

。 。 。 。 。 。 。 。 。 。 。 。 。

。 。 。 。 。 。 。 。 。 。 。 。 。

Making twenty-six points is really fun, and in order to make up four problems, this number-composition problem with only 2626 kinds of weights was created.

Problem Description

You are given a rooted tree TT with nn nodes, rooted at 11. The ii-th node has a color cic_i, where aciz{\tt a} \le c_i \le {\tt z}, and a value PiP_i.

For each node xx, there is a path from it to every node in its subtree (note that the order is from itself to a node in its subtree), for a total of size of subtree\text{size of subtree} paths.

Now, ignore the 22-nd to the PxP_x-th nodes on each of these paths. In particular, if Px=1P_x = 1, then no nodes are ignored. Treat the path after ignoring as a sequence, where the weight of each node in the sequence is the cic_i of the node on the path. For each node, find the maximum, over all its sequences, of the length of the longest non-decreasing subsequence.

Note: If a path jj has lenj<Pxlen_j < P_x nodes, this is equivalent to ignoring all nodes on this path except the first node.

Input Format

The first line contains an integer nn.

The second line contains nn integers PiP_i.

The third line contains nn lowercase letters cic_i, with no spaces between characters.

Next n1n - 1 lines describe the tree TT. Each line contains two integers u,vu, v, meaning there is an edge between uu and vv.

There may be extra spaces at the end of lines (use getchar() with caution).

Output Format

Output nn lines, the answer for each node, in order of increasing index.

7
2 1 1 9 8 5 1
zzabcad
1 2
2 3
3 4
3 5
5 6
5 7
3
3
3
1
1
1
1

12
1 2 2 4 1 3 4 3 1 4 3 1 
baabbbbbbbaa
4 6
5 7
1 2
12 10
8 2
10 11
5 9
10 3
2 3
4 3
4 5

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

Hint

Explanation of Sample 1:

The shape of the tree in the sample:

For node 11: P1=2P_1=2.

Sequence Weights Length of LNDS LNDS
1 z 1 z
1 3 za
1 3 4 zab 2 ab
1 3 5 zac ac
1 3 5 6 zaca
1 3 5 7 zacd 3 acd

The LNDS with the maximum length: acd.

Node 22 is the same as node 11.

For node 33: P3=1P_3=1.

Sequence Weights Length of LNDS LNDS
3 a 1 a
3 4 ab ab
3 5 ac 2 ac
3 5 6 aca
3 5 7 acd 3 acd

The LNDS with the maximum length: acd.

For 4,5,6,74, 5, 6, 7, each of their sequences contains only the node itself, so output 11.

Constraints

This problem uses Subtask scoring.

Subtask Limit Pts
0 n100n \le 100 5
1 n2000n \le 2000 15
2 1inPi=1\forall 1 \le i \le n \quad P_i=1 30
3 Empty 50

For 100%100\% of the testdata:

1n1051 \le n \le 10^5.

1in\forall 1 \le i \le n, cic_i is a lowercase letter, and 1Pin1 \le P_i \le n.

The given tree is connected and valid.

Translated by ChatGPT 5