RiseOJ는 solved.ac와 제휴 관계가 없습니다. 티어 아이콘 © solved.ac. solved.ac
포럼
문제 COCI00699

Harmonija

설명

In the city of Shumen, Bulgaria, there is a large magic tree of \(n\) connected nodes,
where each node hides two magic values: red \(c_{i}\) and blue \(p_{i}\). 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 \(A\) to the ending node \(B\)
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 \(q\) 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 1 harmonic path between
any 2 nodes in the tree.

제약
입력 형식

The first line contains the natural numbers \(n\) and \(q\) (\(1 \le n\), \(q \le 10^{5}\)), the number of nodes in the magic
tree and the number of queries
The second line contains the \(n\) natural numbers \(c_{i}\) (−\(10^{9} \le c_{i} \le 10^{9}\)), the red values of the nodes.
The third line contains the \(n\) natural numbers \(p_{i}\) (−\(10^{9} \le p_{i} \le 10^{9}\)), the blue values of the nodes.
In the next \(n - 1\) lines, there are two natural numbers \(u\) and \(v\) (\(1 \le u\), \(v \le n\), u̸ = \(v\)), there is an edge
between the cities \(u\) and \(v\). There will be a path between any two nodes in the tree.
In the next \(q\) lines, there are two natural numbers \(u\) and \(v\) (\(1 \le u\), \(v \le n\)), the labels of the starting and
ending nodes of the path.

출력 형식

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.

서브태스크
서브태스크점수설명

1

27점

n, \(q \le 15\)

2

41점

n, \(q \le 1000\)

3

19점

\(q \le 10000\)

4

23점

No additional constraints.

예제 1
입력
4 1
10 10 10 10
-10 0 -10 0
1 2
2 3
3 4
1 4
출력
30
예제 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
출력
4
3
3
설명

Clarification of the first example: If we color node 1 red, node 2 blue, and nodes 3 and 4 red,
we get a harmonic path with a value of 30. There is no harmonic path with a higher value because
we have to color at least one of the 4 nodes blue, and the blue values do not increase the sum in this example.

문제 정보

생성자가 기록되지 않았습니다.

출처 COCI 2025/2026 Contest 1

평가 및 의견

Harmonija

개요
출제자 난이도 Unrated 레이팅 미적용 의견 0 / 50 공개 집계 (커뮤니티 난이도, 주요 주제, 품질)는 의견이 충분히 모이면 공개됩니다.

Log in to rate problems.

개별 의견

아직 의견이 없습니다. 자격이 된다면 위 양식에서 가장 먼저 평가해 보세요.

풀이 제출

Harmonija

게스트로 둘러보고 있습니다. 로그인하면 풀이를 제출하고 진행 상황을 확인할 수 있습니다. 로그인하고 제출하기
공개
C++20 Tab 들여쓰기 · Ctrl+/ 주석 토글 · Enter 자동 들여쓰기
1 1 1 0 공백: 4 · UTF-8