Clean Arrangements
문제
In graph drawing, a linear arrangement of a rooted (connected) tree of vertices is a planar drawing where vertices of the tree are placed on a horizontal line, say the -axis, and (n − 1) edges are drawn as semicircular arcs above the line connecting their end vertices as shown in Figure 1. Such linear arrangement π maps each vertex to a distinct integer from to , representing its coordinate along the -axis.
Figure 1. (Left) A rooted tree of nine vertices with the vertex as a root.
(Middle) A clean arrangement of .
(Right) An unclean arrangement of because of the red edge covering the root.
In a linear arrangement π, the distance between two vertices and is defined as the difference of their -coordinates, i.e., d(u, v) = |π(u) − π(v)|. Formally, for a rooted tree , the cost of a linear arrangement π of is defined as .
A clean arrangement π of a rooted tree is a special linear arrangement π satisfying both conditions:
- π has no edge crossings except at common end vertices of edges.
- No edge covers the root vertex of , that is, there is no edge such that π(u) < π(r) < π(v).
For example, the middle in Figure 1 is a clean arrangement of in the left, but the right is not clean because the edge covers the root vertex . The cost of the clean arrangement in the middle is , where there are three edges of distance two and five edges of distance one. This cost is the minimum among all clean arrangements of .
Given a rooted tree with the vertex as a root, write a program to output the minimum possible cost of clean arrangements of the tree.
입력
Your program is to read from standard input. The input starts with a line containing (2 ≤ n ≤ 5\,000), where is the number of vertices of the rooted tree. The vertices are numbered from to , and the root vertex is . In the following (n − 1) lines, each line contains two positive integers and which are end vertices of an (undirected) edge of the tree, where and are distinct integers between and .
출력
Your program is to write to standard output. Print exactly one line. The line should contain the minimum cost of clean arrangements of the tree with root vertex .
예제
예제 1
4 1 2 3 2 2 4
4
예제 2
9 2 4 2 5 7 3 9 7 1 2 3 1 7 8 6 2
11