#P15752. [JAG 2024 Summer Camp #3] Draw the Tree

[JAG 2024 Summer Camp #3] Draw the Tree

题目描述

You are given a tree of NN vertices, numbered 1,2,,N1, 2, \ldots, N. You want to determine a positive integer value MM 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 R\mathbf{R}, 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 p\mathbf{p} from the vertex set of tree T\mathbf{T} 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 viv_i and vjv_j in T\mathbf{T}, p(vi)\mathbf{p}(v_i) and p(vj)\mathbf{p}(v_j) are distinct.
  • For any distinct edges ei=(ui,vi)e_i = (u_i, v_i) and ej=(uj,vj)e_j = (u_j, v_j) in T\mathbf{T}, the line segments segi\mathbf{seg}_i connecting p(ui)\mathbf{p}(u_i) and p(vi)\mathbf{p}(v_i), and segj\mathbf{seg}_j connecting p(uj)\mathbf{p}(u_j) and p(vj)\mathbf{p}(v_j) do not share any points inside segi\mathbf{seg}_i (excluding the endpoints) or inside segj\mathbf{seg}_j (excluding the endpoints).

Your task is to determine if such a drawing is possible by choosing an appropriate MM, and if possible, find the minimum value of MM.

输入格式

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 NN, which is between 11 and 50,00050,000, inclusive.

Each of the following N1N - 1 lines contains two integers uiu_i and viv_i (1ui,viN1 \leq u_i, v_i \leq N). uiu_i and viv_i denote the endpoints of the ii-th edge of T\mathbf{T}.

It is guaranteed that the given graph is a tree.

输出格式

Output a single integer — the minimum possible value of MM to achieve the goal. If it is impossible to do so, output 1-1 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