P3714 [BJOI2017] A Hard Problem on Trees
Description
You are given an unrooted tree with $n$ nodes.
Each edge of the tree has a color. There are $m$ colors, numbered from $1$ to $m$, and the weight of the $i$-th color is $c_i$.
For a simple path on the tree, the edges along the path form a color sequence in order, which can be split into several segments of the same color. Define the path value as the sum of the color weights of each same-color segment in the sequence.
Please compute the maximum path value among all simple paths whose number of edges is between $l$ and $r$.
Input Format
The first line contains four integers $n, m, l, r$.
The second line contains $m$ integers $c_1, c_2, \ldots, c_m$, separated by spaces, representing the weight of each color in order.
The next $n-1$ lines each contain three integers $u, v, c$, indicating that there is an edge between node $u$ and node $v$ with color $c$.
Output Format
Output one line with a single integer, the answer.
Explanation/Hint
Sample explanation 1:
All color weights are negative, and the optimal path is $(1, 2)$ or $(1, 3)$.
Sample explanation 2:
The optimal path is $(3, 1, 2, 5, 6)$, and its color sequence is $(2, 1, 1, 2)$.
Constraints
| Testpoint ID | $n$ | $m$ | Special constraints |
|-|-|-|-|
| $1$ | $=10^3$ | $\le n$ | None |
| $2$ | $=10^4$ | $=2$ | None |
| $3$ | $=10^5$ | $\le n$ | The degree of every node is at most $2$ |
| $4$ | $=2\times10^5$ | $\le n$ | The degree of every node is at most $2$ |
| $5$ | $=10^5$ | $=10$ | $l=1$, $r=n-1$ |
| $6$ | $=2\times10^5$ | $\le n$ | $l=1$, $r=n-1$ |
| $7$ | $=10^5$ | $=50$ | None |
| $8$ | $=10^5$ | $\le n$ | None |
| $9$ | $=2\times 10^5$ | $=100$ | None |
| $10$ | $=2\times 10^5$ | $\le n$ | None |
For $100\%$ of the testdata, $1 \leq n, m \leq 2 \times 10^5$, $1 \leq l \leq r \leq n$, and $\lvert c_i \rvert \leq 10^4$. It is guaranteed that there exists at least one path whose number of edges is between $l$ and $r$.
Translated by ChatGPT 5