Wind of Change
Time limit10sMemory limit1024 MB
Two weighted trees on the same vertex labels define a distance as the sum of both tree distances; for every vertex find the nearest other vertex under that combined metric.
- Level
Hard9 of 10
- Topics
- Tree, Divide and conquer, DFS, Shortest path
- Solved
- No attempts yet
Problem
The original title of this problem is "Tree Product Metric Voronoi Diagram Query Without One Point".
You are given two weighted trees of size , where each vertex is labeled with a number . Let be the total weight of the shortest path from node to in tree , and define the same way.
Consider a point set of size . Similar to the Manhattan metric (in fact, this is a generalization of it), we can define the distance between two points as the sum of two distances, . For each , find the closest point to point . Formally, for each , you should find .
Input
The first line contains a single integer , the number of vertices in both trees. ()
The next lines describe the first tree. Each line contains three integers , which means there is an edge connecting vertices and with weight . ()
The next lines describe the second tree in the same format.
Output
Print lines, each containing a single integer. The -th line holds the answer for point .