Tree and Colors
Time limit2sMemory limit512 MB
Given a rooted tree with colored vertices and queries f(v,c) counting subtree vertices of color at most c, print the sum of all answers modulo 1e9+7.
- Level
Medium6 of 10
- Topics
- Tree, DFS, Sorting, Prefix sum
- Solved
- No attempts yet
Problem
There is a tree with vertices and edges. Vertices are numbered from to , and the root is vertex . There is exactly one path between any two distinct vertices.
Each vertex has one color, represented by an integer between and inclusive. For a vertex and a color , define the query as follows.
is the number of vertices in the subtree rooted at whose color is at most .
queries of the form are given.
Input
The first line contains , , and separated by spaces. is the number of vertices, is the number of queries, and is the number of distinct colors, with , , and .
The second line contains integers separated by spaces. The -th integer is the color of vertex , and each value is between and inclusive.
Each of the following lines describes an edge of the tree. Each line contains two distinct vertex numbers and separated by a space, with .
Each of the following lines describes a query. The -th line contains and separated by a space, with and .
Output
Print the sum of the answers to all queries modulo .