A11037 | The Tag Game
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Alice got tired of playing the tag game by the usual rules so she offered Bob a little modification to it. Now the game should be played on an undirected rooted tree of $n$ vertices. Vertex 1 is the root of the tree.
Alice starts at vertex 1 and Bob starts at vertex $x$ ( $x≠1$ ). The moves are made in turns, Bob goes first. In one move one can either stay at the current vertex or travel to the neighbouring one.
The game ends when Alice goes to the same vertex where Bob is standing. Alice wants to minimize the total number of moves and Bob wants to maximize it.
You should write a program which will determine how many moves will the game last.
Alice starts at vertex 1 and Bob starts at vertex $x$ ( $x≠1$ ). The moves are made in turns, Bob goes first. In one move one can either stay at the current vertex or travel to the neighbouring one.
The game ends when Alice goes to the same vertex where Bob is standing. Alice wants to minimize the total number of moves and Bob wants to maximize it.
You should write a program which will determine how many moves will the game last.
输入格式
The first line contains two integer numbers $n$ and $x$ ( $2<=n<=2·10^{5}$ , $2<=x<=n$ ).
Each of the next $n-1$ lines contains two integer numbers $a$ and $b$ ( $1<=a,b<=n$ ) — edges of the tree. It is guaranteed that the edges form a valid tree.
Each of the next $n-1$ lines contains two integer numbers $a$ and $b$ ( $1<=a,b<=n$ ) — edges of the tree. It is guaranteed that the edges form a valid tree.
输出格式
Print the total number of moves Alice and Bob will make.
输入输出样例
输入 #1
4 3 1 2 2 3 2 4
输出 #1
4
输入 #2
5 2 1 2 2 3 3 4 2 5
输出 #2
6
In the first example the tree looks like this:
The red vertex is Alice's starting position, the blue one is Bob's. Bob will make the game run the longest by standing at the vertex 3 during all the game. So here are the moves:
B: stay at vertex 3
A: go to vertex 2
B: stay at vertex 3
A: go to vertex 3
In the second example the tree looks like this:
The moves in the optimal strategy are:
B: go to vertex 3
A: go to vertex 2
B: go to vertex 4
A: go to vertex 3
B: stay at vertex 4
A: go to vertex 4
The red vertex is Alice's starting position, the blue one is Bob's. Bob will make the game run the longest by standing at the vertex 3 during all the game. So here are the moves:
B: stay at vertex 3
A: go to vertex 2
B: stay at vertex 3
A: go to vertex 3
In the second example the tree looks like this:
The moves in the optimal strategy are:
B: go to vertex 3
A: go to vertex 2
B: go to vertex 4
A: go to vertex 3
B: stay at vertex 4
A: go to vertex 4
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted