Simple Tree Problem
Practice
5 (4 votes)
Algorithms
Approved
Hard
Problem
70% Success 171 Attempts 50 Points 3s Time Limit 256MB Memory 1024 KB Max Code

You are given a tree with N nodes. Each node i has a value \(A_{i}\) associated with it. You are asked to perform Q queries on it.
Each query contains an integer i.
In each query, you need to remove the \(i^{th}\) edge and find number of connected unordered pair of nodes \(u, v\) such that \(|A_u - A_v| \leq D\).

Note :
The tree is restored to it's original state after each query.

Input :
The first line of the input contains three single space separated integers \(N, D\) and Q.
The next line of the input contains N single space separated integers denoting \(A_i\).
Each of the next \(N - 1\) lines contains two space separated integers u and v , the nodes that the \(i^{th}\) edge connects (Edges are ordered by 1 based index).
Each of the next Q lines of the input contains an integer i corresponding to that query.

Output :
For each query, print the total number of connected unordered pairs \(u, v\) such that \(| A_u - A_v | \leq D\).

Constraints :

  • \(2 ≤ N ≤ 10^5\)
  • \(1 ≤ A_i ≤ 10^6\)
  • \(1 ≤ Q ≤ 10^5\)
  • \(0 ≤ D ≤ 20\)

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
1 votes
Tags:
AlgorithmsApprovedHardSqrt-Decomposition
Points:50
2 votes
Tags:
AlgorithmsHardSquare Root Decomposition
Points:50
2 votes
Tags:
AlgorithmsApprovedData StructuresHardPrime FactorizationTrees