No.1752 Up-Down Tree
Problem Statement
You are given a tree with $N$ vertices rooted at vertex $1$. Using this tree and $1$ piece, you perform the following operations.
- First, choose $1$ vertex and place the piece on it.
- Repeatedly move the piece to a vertex on which it has never been placed. Here, counting the move right after the initial placement of the piece as the $1$st move, on odd-numbered moves the piece is moved to "a vertex that is an ancestor of the vertex where the piece currently is", and on even-numbered moves it is moved to "a vertex that is a descendant of the vertex where the piece currently is".
- If there is no vertex to which the piece can be moved, the operations end.
Here, among the $N-1$ edges of the tree with $N$ vertices, the $i$-th edge connects $A_i$ and $B_i$ given in the input. Also, for vertices $u$, $v$, both "$u$ is an ancestor of $v$" and "$v$ is a descendant of $u$" mean that "$u$ lies on the shortest path connecting $1$ and $v$".
Input
$N$
$A_1\ B_1$
$A_2\ B_2$
...
$A_{N-1}\ B_{N-1}$
- $1\leq N\leq 10^5$
- $1\leq A_i,B_i\leq N$ $(1\leq i\leq N-1)$
- The given graph is a tree
- All inputs are integers
Output
Output the maximum score you can obtain as an integer. Print a newline at the end.
Sample
Sample 1
Input
7 1 2 1 3 2 4 4 5 4 6 4 7
Output
7
The given tree has the structure shown in the figure below. For example, by moving the piece in the order $3$→$1$→$7$→$4$→$6$→$2$→$5$, you can make the piece be placed on every vertex at some point. Note that while placing the piece on the same vertex twice is not allowed, it is possible to move the piece by jumping over vertices on which it has already been placed.
Sample 2
Input
9 1 2 2 3 2 4 6 5 7 5 1 5 8 1 8 9
Output
7
The given tree has the structure shown in the figure below. By performing the operations in an appropriate order, you can place the piece once on every vertex except $8$,$9$. If you place the piece on $8$,$9$ at least once, the score will be $6$ points or less no matter what you do, so $7$ points is the best.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
riano
sak