Two Trees
Time limit5sMemory limit1024 MB
For each vertex i, find the maximum a_k over vertices k that lie in the subtree of i in both trees.
- Level
Medium7 of 10
- Topics
- Tree, DFS, Segment tree
- Solved
- No attempts yet
Problem
Two trees , and a sequence are given. Each tree has vertices, and the vertices are numbered from 1 to with no repeats. The root of both trees is vertex 1.
In and , let and be the sets of vertex numbers in the subtree rooted at vertex , respectively. Define .
Write a program that computes .
Input
The first line contains .
The second line contains the integers , separated by spaces.
The next lines each contain two integers and , separated by a space, that are the endpoints of an edge of .
The next lines each contain two integers and , separated by a space, that are the endpoints of an edge of .
It is guaranteed that and are trees.
Output
Print , one per line, in order.