The Unstable Root
Practice
5 (1 votes)
Loops
Data structures
Centroid decomposition
Tree
Medium Hard
Problem
48% Success 197 Attempts 50 Points 3s Time Limit 256MB Memory 1024 KB Max Code

You are given a tree comprising \(n\) nodes numbered from \(1\) to \(n\). You have to answer \(q\) queries each consisting of three numbers – \(r\)\(d_1\) and \(d_2\)

For each query, print the number of nodes whose depth is between \(d_1\) and \(d_2\) (inclusive) if the tree is rooted at node \(r\). The depth of root node itself is \(0\), the depth of all its children is \(1\) and so on.

Input format

First line: \(n\) (the number of nodes) and \(q\) (the number of queries)

Next \(n-1\) lines: \(u_i\) and \(v_i\)denoting an edge between the nodes \(u_i\) and \(v_i\).The next \(q\) lines consist of three integers \(r\)\(d_1\) and \(d_2\) .

Output format

For each query, print the answer in a new line.

input Constraints

\(1 \le n \le 100000\\1 \le q \le 150000\\ 0 \le d_1 \le d_2 \le n\\1 \le r \le n\)

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
7 votes
Tags:
Dynamic ProgrammingXORImplementationTreeTreesCombinatoricsData StructuresLowest Common AncestorMathematics
Points:50
5 votes
Tags:
Data StructuresLowest Common AncestorTreesLCA
Points:50
1 votes
Tags:
Dynamic ProgrammingData StructuresTreesLowest Common Ancestor