COCI '25 Contest 1 #3 Harmonija
View as PDF
In the city of Shumen, Bulgaria, there is a large magic tree of n connected nodes, where each node hides two magic values: red and blue
. Each value represents the power that the node gives depending on its chosen color. Your goal is to explore the harmonious paths in the tree.
Let's consider the shortest path from the starting node to the ending node
in the tree. We traverse the nodes in order and choose either blue or red color for the current node. After choosing the color of the current node, the state of the path must be harmonious, i.e. no color may "mogg" another. We say that one color "moggs" another if we have chosen it at least three more times than the other color. This rule must hold at every moment of the path for the path to be considered harmonious.
We define the value of a path as the sum of the values of all nodes on that path, where each node's value is its magic value of the color it is colored with.
Your task is to determine the maximum value of paths given with a starting and ending node if the paths must be harmonic.
According to the given definition, it can be shown that there is always at least harmonic path between any
nodes in the tree.
Input Specification
The first line contains the natural numbers and
, the number of nodes in the magic tree and the number of queries.
The second line contains the natural numbers
, the red values of the nodes.
The third line contains the natural numbers
, the blue values of the nodes.
In the next lines, there are two natural numbers
and
, there is an edge between the cities
and
. There will be a path between any two nodes in the tree.
In the next lines, there are two natural numbers
and
, the labels of the starting and ending nodes of the path.
Output Specification
For each query, print one number on a separate line - the highest value of the harmonic path between the starting and ending nodes of the query.
Constraints
| Subtask | Points | Constraints |
|---|---|---|
| 1 | 27 | |
| 2 | 41 | |
| 3 | 19 | |
| 4 | 23 | No additional constraints. |
Sample Input 1
4 1
10 10 10 10
-10 0 -10 0
1 2
2 3
3 4
1 4
Sample Output 1
30
Explanation for Sample Output 1
If we color node red, node
blue, and nodes
and
red, we get a harmonic path with a value of
. There is no harmonic path with a higher value because we have to color at least one of the
nodes blue, and the blue values do not increase the sum in this example.
Sample Input 2
5 3
-5 -4 0 -3 3
3 1 -5 0 0
3 2
1 4
3 5
1 2
2 5
1 4
5 3
Sample Output 2
4
3
3
Comments