PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000055. Misha, Grisha và tàu điện ngầm (Misha, Grisha and Underground)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Misha, Grisha và tàu điện ngầm (Misha, Grisha and Underground)

Misha, Grisha and Underground

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A metro network consists of nn stations connected as a tree. Each day, three given stations a,b,ca,b,c are assigned the roles of Misha's start ss, Grisha's start tt, and their common destination ff.

Misha travels along the shortest path from ss to ff and marks every station visited, endpoints included. Grisha then travels from tt to ff and counts marked stations on his path. Choose an assignment of the three stations that maximizes this count. The three provided stations need not be distinct.

Input

  • The first line contains n,qn,q.
  • The next line contains n−1n-1 integers p2,…,pnp_2,\ldots,p_n, defining an edge between each ii and pip_i.
  • The next qq lines each contain three stations a,b,ca,b,c.

Output

Print the maximum possible number of marked stations Grisha can visit for each day.

Subtasks

General constraints: 2≤n,q≤1052\le n,q\le10^5; 1≤a,b,c,pi≤n1\le a,b,c,p_i\le n; các cạnh tạo thành cây.

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; 1≤q≤501\le q\le 50.

  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; 1≤q≤10001\le q\le 1000.

  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤q≤1000001\le q\le 100000.

Examples

Input

3 2
1 1
1 2 3
2 3 3

Output

2
3

Explanation

The edges are 1−21-2 and 1−31-3. For (1,2,3)(1,2,3), assign s=1,f=2,t=3s=1,f=2,t=3: Misha marks 1,21,2 and Grisha's route 3−1−23-1-2 sees two marks. For (2,3,3)(2,3,3), assign s=t=3,f=2s=t=3,f=2; both follow 3−1−23-1-2, giving three marked stations.