Tree Reading
InterviewTime limit1sMemory limit1024 MB
Given a tree whose vertices each hold a digit from 1 to 9, sum the numbers read along the path for every ordered pair of vertices, modulo 1,000,000,007.
- Level
Medium6 of 10
- Topics
- Tree, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
A tree with vertices is given. The vertices are numbered from to . Vertex has a digit , which is one of the numbers from to .
For vertices and of the tree, let be the decimal integer formed by writing down the digits on the vertices of the shortest path from vertex to vertex , in path order. For example, if vertices , , and have the numbers , , and , and the shortest path from vertex to vertex is , then .
Find the sum of over all possible integer pairs (), modulo .
Input
The first line contains . ()
The second line contains , separated by spaces. ()
Each of the next lines contains two vertex numbers and , separated by a space, meaning that vertices and are connected by an edge. ()
All numbers in the input are integers.
Output
Print the answer on the first line.