atcoder#ARC152F. [ARC152F] Attraction on Tree
[ARC152F] Attraction on Tree
Score : points
Problem Statement
You are given a tree with vertices numbered to . The -th edge connects two vertices and .
Initially, a piece is placed at vertex . You will perform the following operation exactly times.
- Choose a vertex that is not occupied by the piece at that moment and is never chosen in the previous operations, and move the piece one vertex toward the chosen vertex.
A way to perform the operation times is called a good procedure if the piece ends up at vertex . Additionally, a good procedure is called an ideal procedure if the number of vertices visited by the piece at least once during the procedure (including vertices and ) is the minimum possible.
Find the number of vertices visited by the piece at least once during an ideal procedure.
If there is no good procedure, print -1
instead.
Constraints
- All values in the input are integers.
- The given graph is a tree.
Input
The input is given from Standard Input in the following format:
Output
Print the answer as an integer.
4
1 2
2 4
3 4
3
If you choose vertices , , , in this order, the piece will go along the path → → → → . This is an ideal procedure.
6
1 6
2 6
2 3
3 4
4 5
-1
There is no good procedure.
14
1 2
1 3
3 4
3 5
5 6
6 7
5 8
8 9
8 14
14 10
10 11
14 12
12 13
5