Simple Tree Problem

5

4 votes
Algorithms, Approved, Hard
Problem

You are given a tree with N nodes. Each node i has a value Ai 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 ith edge and find number of connected unordered pair of nodes u,v such that |AuAv|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 Ai.
Each of the next N1 lines contains two space separated integers u and v , the nodes that the ith 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 |AuAv|D.

Constraints :

  • 2N105
  • 1Ai106
  • 1Q105
  • 0D20

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

In the 3rd query, i.e when the 2nd edge is removed, the pairs {1,2},{4,5} and {4,6} satisfies the required conditions.

Editor Image

?