CF832D.Misha, Grisha and Underground

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

米沙和格里沙是两个有趣的男孩,因此他们喜欢使用新的地铁系统。该地铁系统共有 nn 个车站,由 n−1n-1 条线路连接,每条线路连接两个车站,且任意两个车站之间均可通过线路相互到达(即整个地铁网络构成一棵树)。

两个男孩决定玩一个有趣的游戏,并制定了如下计划:某天早晨,米沙将从车站 ss 沿最短路径前往车站 ff,并在他途经的每一个车站(包括起点 ss 和终点 ff)用喷漆写下难看的文字“Misha was here”。当天晚上,格里沙将从车站 tt 沿最短路径前往车站 ff,并统计途中经过的、已被米沙写上文字的车站数量。当晚深夜,地铁工作人员会将所有文字清洗干净,以确保地铁环境整洁。

两个男孩已为接下来的若干天各自选定三座车站 aa、bb 和 cc;每天需从中指定一座作为 ss,一座作为 ff,剩下的一座作为 tt。他们很想知道:应如何为每一天分配 ss、ff、tt,才能使格里沙所统计到的、被米沙写过文字的车站数量最大化?他们请你帮忙解决这个问题。

输入格式

The first line contains two integers n and q (2 ≤ n ≤ 105, 1 ≤ q ≤ 105) — 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.

第一行包含两个整数 nn 和 qq(2≤n≤1052 \leq n \leq 10^5,1≤q≤1051 \leq q \leq 10^5)—— 分别表示车站的数量和天数。

第二行包含 n−1n-1 个整数 p2, p3, …, pnp_2,\ p_3,\ \dots,\ p_n(1≤pi≤n1 \leq p_i \leq n)。整数 pip_i 表示车站 pip_i 与车站 ii 之间存在一条线路。保证任意两个车站之间均可互相到达。

接下来的 qq 行,每行包含三个整数 aa、bb 和 cc(1≤a, b, c≤n1 \leq a,\ b,\ c \leq n)—— 表示某一天三位男孩各自选择的车站编号。注意:这些编号中可能存在重复。

输出格式

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.

输出 q 行。在第 i 行中,输出当在第 i 天的三个车站中最优地选择车站 s、t 和 f 时,Grisha 能得到的最大可能数值。

输入输出样例

  • 输入#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.

在第一个样例中,第一天若 s=1s = 1、f=2f = 2、t=3t = 3,米沙将沿路线 11 22 行进,而格里沙将沿路线 33 11 22 行进。他将在车站 11 和 22 看到文字。第二天,若 s=3s = 3、f=2f = 2、t=3t = 3,两名男孩都将沿路线 33 11 22 行进。格里沙将在 33 个车站看到文字。

在第二个样例中,若 s=1s = 1、f=3f = 3、t=2t = 2,米沙将沿路线 11 22 33 行进,而格里沙将沿路线 22 33 行进,并将在两个车站均看到文字。

输入解题思路,AI测评打分。不知道怎么写?

首页