Region Find
Practice
3.8 (23 votes)
Depth first search
Medium
Trees
Problem
57% Success 284 Attempts 50 Points 2s Time Limit 256MB Memory 1024 KB Max Code

You are given a tree with N nodes and N-1 edges. We define a region as a set of nodes, such that if we remove all other nodes from the tree, these nodes still remain connected i.e. there exists a path between any two remaining nodes.

All the nodes have a weight associated to them. You are given Q queries. Each query has a number k. You have to find number of regions, such that the minimum weight among all the nodes of that region is exactly k. Output your answer modulo 109 + 7.

INPUT

The first line will contain N, the number of nodes. Next line will contain N integers. The ith integer will indicate weight, the weight of ith node. Next N-1 lines will contain 2 integers "x y" (quotes for clarity) denoting an edge between node x and node y. Next line will contain a number Q , the number of queries. Next Q lines will contain an integer k , which is as described in problem statement.

NOTE : nodes are numbered 1 indexed.

OUTPUT

You have to output Q lines, each containing a single integer, the answer to the corresponding query.

CONSTRAINTS

1 ≤ N ≤ 1500

1≤ x, y ≤ n

1 ≤ Q ≤ 2000

1 ≤ weight, k ≤ 109

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
12 votes
Tags:
AlgorithmsDepth First SearchGraphs
Points:50
37 votes
Tags:
Data StructuresDisjoint setGraphsMapsMediumReadyTrees
Points:50
Tags:
AlgorithmsGraphsMedium