問題一覧 > 通常問題

No.1752 Up-Down Tree

レベル : / AI推定難易度 : 2600 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 12
作問者 : riano / テスター : sak
お気に入りにしたユーザー ProblemId : 6493 / 自分の提出
問題文最終更新日: 2021-12-30 00:08:03
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 323 (順位表) の他の問題:

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.

  1. First, choose $1$ vertex and place the piece on it.
  2. 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".
  3. If there is no vertex to which the piece can be moved, the operations end.
Your score is the number of vertices on which the piece has been placed during this series of operations, performed only once until it ends. Find the maximum score you can obtain.
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もしくは右上の雲マークをクリックしてアカウントを作成してください。