CF832D.Misha, Grisha and Underground

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

Misha and Grisha are funny boys, so they like to use new underground. The underground has nn stations connected with n1n-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 ss to station ff by the shortest path, and will draw with aerosol an ugly text "Misha was here" on every station he will pass through (including ss and ff ). After that on the same day at evening Grisha will ride from station tt to station ff 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 aa , bb and cc for each of several following days, one of them should be station ss on that day, another should be station ff , and the remaining should be station tt . They became interested how they should choose these stations ss , ff , tt so that the number Grisha will count is as large as possible. They asked you for help.

输入格式

The first line contains two integers nn and qq ( 2<=n<=1052<=n<=10^{5} , 1<=q<=1051<=q<=10^{5} ) — the number of stations and the number of days.

The second line contains n1n-1 integers p2,p3,...,pnp_{2},p_{3},...,p_{n} ( 1<=pi<=n1<=p_{i}<=n ). The integer pip_{i} means that there is a route between stations pip_{i} and ii . It is guaranteed that it's possible to reach every station from any other.

The next qq lines contains three integers aa , bb and cc each ( 1<=a,b,c<=n1<=a,b,c<=n ) — the ids of stations chosen by boys for some day. Note that some of these ids could be same.

输出格式

Print qq lines. In the ii -th of these lines print the maximum possible number Grisha can get counting when the stations ss , tt and ff are chosen optimally from the three stations on the ii -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 ss = 11 , ff = 22 , tt = 33 , Misha would go on the route 11 22 , and Grisha would go on the route 33 11 22 . He would see the text at the stations 11 and 22 . On the second day, if ss = 33 , ff = 22 , tt = 33 , both boys would go on the route 33 11 22 . Grisha would see the text at 33 stations.

In the second examle if ss = 11 , ff = 33 , tt = 22 , Misha would go on the route 11 22 33 , and Grisha would go on the route 22 33 and would see the text at both stations.

首页