Region Find
Tag(s):

Depth-first search, Medium-Hard, Tree

Problem
Editorial
Analytics

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

SAMPLE INPUT
4
2 4 3 1
4 3
4 2
4 1
4
1
2
3
4
SAMPLE OUTPUT
8
1
1
1
Time Limit: 2.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: Bash, C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, TypeScript, Visual Basic

CODE EDITOR

Initializing Code Editor...

This Problem was Asked in Challenge Name

December Easy '15

OTHER PROBLEMS OF THIS CHALLENGE
• Algorithms > Sorting
• Algorithms > Greedy Algorithms
• Algorithms > Graphs
• Algorithms > Greedy Algorithms