#P15752. [JAG 2024 Summer Camp #3] Draw the Tree
[JAG 2024 Summer Camp #3] Draw the Tree
题目描述
You are given a tree of vertices, numbered . You want to determine a positive integer value and draw this tree on a region $\mathbf{R} = \{(x, y) \mid 0 \leq x \leq M - 1, 0 \leq y \leq 1\}$ on a two-dimensional coordinate plane.
In this drawing, the vertices of the tree should be placed at grid points within , and the edges of the tree should be drawn as straight line segments. Furthermore, these line segments representing different edges of the tree should not share any points other than their endpoints.
More formally, you want to construct a mapping from the vertex set of tree to the set of grid points $\{(x, y) \mid x \in \{0, 1, \ldots, M - 1\}, y \in \{0, 1\}\}$ such that the following properties are satisfied:
- For any distinct vertices and in , and are distinct.
- For any distinct edges and in , the line segments connecting and , and connecting and do not share any points inside (excluding the endpoints) or inside (excluding the endpoints).
Your task is to determine if such a drawing is possible by choosing an appropriate , and if possible, find the minimum value of .
输入格式
The input consists of a single test case of the following format.
$$\begin{aligned} &N \\ &u_1 \ v_1 \\ &u_2 \ v_2 \\ &\vdots \\ &u_{N-1} \ v_{N-1} \end{aligned} $$The first line consists of an integer , which is between and , inclusive.
Each of the following lines contains two integers and (). and denote the endpoints of the -th edge of .
It is guaranteed that the given graph is a tree.
输出格式
Output a single integer — the minimum possible value of to achieve the goal. If it is impossible to do so, output as the answer.
6
1 2
1 3
1 4
1 5
1 6
3
21
1 2
1 3
1 4
1 5
6 7
6 8
6 9
6 10
11 12
11 13
11 14
11 15
16 17
16 18
16 19
16 20
5 21
10 21
15 21
20 21
-1
18
1 2
2 3
2 4
2 5
1 6
6 7
6 8
6 9
1 12
10 11
11 12
12 13
13 14
1 16
15 16
16 17
17 18
11
21
20 10
7 2
7 17
18 5
3 1
15 17
11 7
14 18
11 21
6 2
8 19
17 14
4 18
16 17
9 10
19 17
18 12
15 3
13 15
16 10
11