P14622 [2019 KAIST RUN Fall] Wind of Change
Description
The original title of this problem is "Tree Product Metric Voronoi Diagram Query Without One Point".
You are given two weighted trees $T_1,\ T_2$ of size $N$, where each vertex are labeled with numbers $1 \ldots N$. Let $dist(T_1,\ i,\ j)$ be the total weight of the shortest path from node $i$ to $j$ in tree $T_1$, and define $dist(T_2,\ i,\ j)$ similarly.
Consider a point set of size $N$. Similar to Manhattan metric (in fact, this is a generalization of it), we can define the distance between two points $1 \le i,\ j \le N$: It is the sum of two distances, $dist(T_1,\ i,\ j) + dist(T_2,\ i,\ j)$. For each $1 \le i \le N$, please determine the "closest point" from the point $i$. Formally, for each $i$, you should find $\min_{j \neq i}{dist(T_1,\ i,\ j) + dist(T_2,\ i,\ j)}$.
Input Format
In the first line, a single integer $N$ denoting the number of vertices in both trees is given. ($2 \le N \le 250\,000$)
In the next $N-1$ lines, description of the first tree is given. Each of the $N-1$ lines contains three integers $S_i, E_i, W_i$, which indicates there is an edge connecting two vertices $S_i, E_i$ with weight $W_i$ ($1 \le S_i, E_i \le N, 1 \le W_i \le 10^9$)
In the next $N-1$ lines, description of the second tree is given in the same format.
Output Format
Print $N$ lines containing a single integer. In the $i$-th line, you should print a single integer denoting the answer for the point $i$.