Count Nodes

Time: 4.0 s     Memory: 1024 MB
  • A tree on $N$ nodes labeled with integers between $0$ and $N-1$ is given. Your task is to answer the following query:

    • Given the node $u$ and the integer $d$, count the number of unique nodes that can be reached from $u$ by crossing at most $d$ edges.

    Input

    The first line of input contains the integers $N$ and $Q$ ($1 \leq N,Q \leq 2*10^5$), the number of nodes and queries.

    The following $N-1$ lines each contain the integers $a$ and $b$ ($0 \leq a,b \leq N-1$), which means that there is an edge between node $a$ and $b$. These edges are guaranteed to form a tree.

    Then, the next $Q$ each contain a query.

    Each query consists of the integers $u$ and $d$ ($1 \leq u,d \leq N-1$), the starting node and the number of edges you may cross.

    Output

    For each query, print the number of unique nodes that can be reached from $u$ by crossing at most $d$ edges.

    Scoring

    Your solution will be tested on multiple testcases. To solve the problem, your solution must solve every testcase correctly.

    Sample Input 1 Sample Output 1
    5 5
    0 1
    1 2
    2 3
    2 4
    0 0
    0 1
    0 2
    2 1
    2 2
    
    1
    2
    3
    4
    5
    
Centroid decomposition
  •  
  • To solve the problems, you can either start a virtual contest or register for regular practice. A virtual contest simulates a participation in the original contest with a duration of 1 day, while regular practice lets you submit solutions without any constraints.

    You must log in to register.
  • Close Node
  • Count Nodes
  • Faraway Node
You must log in to submit solutions to the problem.
{"contest_start_timestamp": null, "contest_duration": 86400, "contest_started": true, "contest_ended": true, "flexible_start_window_end_time": null, "only_virtual": true}