#P14671. [ICPC 2025 Seoul R] Clean Arrangement
[ICPC 2025 Seoul R] Clean Arrangement
题目描述
In graph drawing, a 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 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.
:::align{center}

Figure 1. (Left) A rooted tree of nine vertices with the vertex 1 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., . Formally, for a rooted tree , the cost of a linear arrangement of is defined as .
A 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 .
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 1. The cost of the clean arrangement in the middle is 11, 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 1 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 (), where is the number of vertices of the rooted tree. The vertices are numbered from 1 to , and the root vertex is 1. In the following 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 1 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.
The following shows sample input and output for two test cases. The second test case corresponds to Figure 1.
4
1 2
3 2
2 4
4
9
2 4
2 5
7 3
9 7
1 2
3 1
7 8
6 2
11