← 블로그 돌아가기

[AtCoder | DP] D - Ki

컴퓨터 과학 > 알고리즘

2026-09-221분 읽기

#computer-science #algorithm #Dynamic Programming

문제 링크

Problem Statement

Given is a rooted tree with NN vertices numbered 11 to NN. The root is Vertex 11, and the ii-th edge (1iN11 \leq i \leq N - 1) connects Vertex aia_i and bib_i.
Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 00.
Now, the following QQ operations will performed:


Constraints


Input

NQa1b1aN1bN1p1x1pQxQ\boxed{ \begin{array}{l} N \quad Q \\ a_1 \quad b_1 \\ \vdots \\ a_{N-1} \quad b_{N-1} \\ p_1 \quad x_1 \\ \vdots \\ p_Q \quad x_Q \end{array} }

Solution

graph TD
    A((1)) --- B((2))
    B --- C((3))
    B --- D((4))

    class A,B,C,D node;