A11164 | Misha, Grisha and Underground
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Misha and Grisha are funny boys, so they like to use new underground. The underground has $n$ stations connected with $n-1$ routes so that each route connects two stations, and it is possible to reach every station from any other.
The boys decided to have fun and came up with a plan. Namely, in some day in the morning Misha will ride the underground from station $s$ to station $f$ by the shortest path, and will draw with aerosol an ugly text "Misha was here" on every station he will pass through (including $s$ and $f$ ). After that on the same day at evening Grisha will ride from station $t$ to station $f$ by the shortest path and will count stations with Misha's text. After that at night the underground workers will wash the texts out, because the underground should be clean.
The boys have already chosen three stations $a$ , $b$ and $c$ for each of several following days, one of them should be station $s$ on that day, another should be station $f$ , and the remaining should be station $t$ . They became interested how they should choose these stations $s$ , $f$ , $t$ so that the number Grisha will count is as large as possible. They asked you for help.
The boys decided to have fun and came up with a plan. Namely, in some day in the morning Misha will ride the underground from station $s$ to station $f$ by the shortest path, and will draw with aerosol an ugly text "Misha was here" on every station he will pass through (including $s$ and $f$ ). After that on the same day at evening Grisha will ride from station $t$ to station $f$ by the shortest path and will count stations with Misha's text. After that at night the underground workers will wash the texts out, because the underground should be clean.
The boys have already chosen three stations $a$ , $b$ and $c$ for each of several following days, one of them should be station $s$ on that day, another should be station $f$ , and the remaining should be station $t$ . They became interested how they should choose these stations $s$ , $f$ , $t$ so that the number Grisha will count is as large as possible. They asked you for help.
输入格式
The first line contains two integers $n$ and $q$ ( $2<=n<=10^{5}$ , $1<=q<=10^{5}$ ) — the number of stations and the number of days.
The second line contains $n-1$ integers $p_{2},p_{3},...,p_{n}$ ( $1<=p_{i}<=n$ ). The integer $p_{i}$ means that there is a route between stations $p_{i}$ and $i$ . It is guaranteed that it's possible to reach every station from any other.
The next $q$ lines contains three integers $a$ , $b$ and $c$ each ( $1<=a,b,c<=n$ ) — the ids of stations chosen by boys for some day. Note that some of these ids could be same.
The second line contains $n-1$ integers $p_{2},p_{3},...,p_{n}$ ( $1<=p_{i}<=n$ ). The integer $p_{i}$ means that there is a route between stations $p_{i}$ and $i$ . It is guaranteed that it's possible to reach every station from any other.
The next $q$ lines contains three integers $a$ , $b$ and $c$ each ( $1<=a,b,c<=n$ ) — the ids of stations chosen by boys for some day. Note that some of these ids could be same.
输出格式
Print $q$ lines. In the $i$ -th of these lines print the maximum possible number Grisha can get counting when the stations $s$ , $t$ and $f$ are chosen optimally from the three stations on the $i$ -th day.
输入输出样例
输入 #1
3 2 1 1 1 2 3 2 3 3
输出 #1
2 3
输入 #2
4 1 1 2 3 1 2 3
输出 #2
2
In the first example on the first day if $s$ = $1$ , $f$ = $2$ , $t$ = $3$ , Misha would go on the route $1$  $2$ , and Grisha would go on the route $3$  $1$  $2$ . He would see the text at the stations $1$ and $2$ . On the second day, if $s$ = $3$ , $f$ = $2$ , $t$ = $3$ , both boys would go on the route $3$  $1$  $2$ . Grisha would see the text at $3$ stations.
In the second examle if $s$ = $1$ , $f$ = $3$ , $t$ = $2$ , Misha would go on the route $1$  $2$  $3$ , and Grisha would go on the route $2$  $3$ and would see the text at both stations.
In the second examle if $s$ = $1$ , $f$ = $3$ , $t$ = $2$ , Misha would go on the route $1$  $2$  $3$ , and Grisha would go on the route $2$  $3$ and would see the text at both stations.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted