← 블로그 돌아가기
[AtCoder | DP] D - Ki
컴퓨터 과학 > 알고리즘
2026-09-221분 읽기
#computer-science #algorithm #Dynamic Programming
문제 링크
Problem Statement
Given is a rooted tree with N vertices numbered 1 to N. The root is Vertex 1, and the i-th edge (1≤i≤N−1) connects Vertex ai and bi.
Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0.
Now, the following Q operations will performed:
- Operation j(1≤j≤Q): Increment by xj the counter on every vertex contrained in the subtree rooted at Vertex pj.
Find the value of the counter on each vertex after all operations.
Constraints
- 2≤N≤2×105
- 1≤Q≤2×105
- 1≤ai<bi≤N
- 1≤pj≤N
- 1≤xj≤104
Input
NQa1b1⋮aN−1bN−1p1x1⋮pQxQ
Solution
graph TD
A((1)) --- B((2))
B --- C((3))
B --- D((4))
class A,B,C,D node;