#ABC148F. Playing Tag on Tree
Playing Tag on Tree
题目描述
We have a tree with vertices. The -th edge connects Vertex and bidirectionally.
Takahashi is standing at Vertex , and Aoki is standing at Vertex .
Now, they will play a game of tag as follows:
-
. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Takahashi moves to a vertex of his choice that is adjacent to his current vertex.
-
. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Aoki moves to a vertex of his choice that is adjacent to his current vertex.
-
. Go back to step .
Takahashi performs his moves so that the game ends as late as possible, while Aoki performs his moves so that the game ends as early as possible.
Find the number of moves Aoki will perform before the end of the game if both Takahashi and Aoki know each other's position and strategy.
It can be proved that the game is bound to end.
我们有一棵有 个顶点的树。第 条边双向连接顶点 和 。
高桥站在顶点 ,青木站在顶点 。
现在,他们将进行如下的捉人游戏:
.如果高桥和青木站在同一个顶点,游戏结束。否则,高桥移动到他选择的与当前顶点相邻的顶点。
.如果高桥和青木站在同一顶点,对局结束。否则,青木移动到自己选择的与当前顶点相邻的顶点。
.回到步骤 。
高桥的下法是尽可能晚地结束对局,而青木的下法是尽可能早地结束对局。
如果高桥和青木都知道对方的位置和策略,求青木在对局结束前要下的步数。
可以证明对局必将结束。
输入格式
输入内容按以下格式标准输入:
输出格式
打印青木在对局结束前要走的步数。
样例 #1
样例输入 #1
5 4 1
1 2
2 3
3 4
3 5
样例输出 #1
2
样例 #2
样例输入 #2
5 4 5
1 2
1 3
1 4
1 5
样例输出 #2
1
样例 #3
样例输入 #3
2 1 2
1 2
样例输出 #3
0
样例 #4
样例输入 #4
9 6 1
1 2
2 3
3 4
4 5
5 6
4 7
7 8
8 9
样例输出 #4
5
说明
数据规模与约定
- 给定图形是一棵树。
样例 解释
如果两位棋手都以最佳状态下棋,棋局的进展情况如下:
- 高桥走到顶点 。
- 青木移至顶点 。
- 高桥(Takahashi)下到顶点 。
- 青木移动到顶点 。
- 高桥移动到顶点 。
在这里,青木下了两步棋。
需要注意的是,每一步都禁止停留在当前顶点。