(LCA)Codeforces Round #425 (Div. 2) D - Misha, Grisha and Underground

D. Misha, Grisha and Underground
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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 p2, p3, ..., pn (1 ≤ pi ≤ n). The integer pi means that there is a route between stations pi 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.

Output

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.

Examples
Input
3 2
1 1
1 2 3
2 3 3
Output
2
3
Input
4 1
1 2 3
1 2 3
Output
2
Note

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.

 

树上a到b的最短路径就是 从a,b的LCA走向b的这条唯一路径。

对于本题,分别求出(a,b),(b,c),(c,a)的LCA 为x,y,z  不妨设其中深度最大的为x 即(a,b)的LCA。

若f为c,则此时的公共距离为从x到c的路径所经过的点数。

若f为a或b,不妨设为a,则此时的公共距离为 从x到a的路径所经过的点数 与从y到a的路径所经过的点数中的较小的一个。因为x为深度最大的,a点的深度为一定值(且a、x所求得的值就是深度差+1),故x出发的必然更小。

因此,只需要求两两LCA中深度最大的到三个点中最大的路径经过的点数即可。

  1 #include <iostream>
  2 #include <string>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <cstdio>
  6 #include <cmath>
  7 #include <queue>
  8 #include <set>
  9 #include <map>
 10 #include <list>
 11 #include <vector>
 12 #include <stack>
 13 #define mp make_pair
 14 //#define P make_pair
 15 #define MIN(a,b) (a>b?b:a)
 16 //#define MAX(a,b) (a>b?a:b)
 17 typedef long long ll;
 18 typedef unsigned long long ull;
 19 const int MAX=1e5+5;
 20 const int INF=1e8+5;
 21 using namespace std;
 22 //const int MOD=1e9+7;
 23 typedef pair<ll,int> pii;
 24 const double eps=0.00000001;
 25 const int MAX_V=1e5+5;
 26 const int MAX_LOG_V=20;
 27 vector<int> G[MAX_V];//图的邻接表表示
 28 int root;//根节点编号
 29 int parent[MAX_LOG_V][MAX_V];//向上走2^k步时到达的节点   (超过根时记为-1)
 30 int depth[MAX_V];//节点的深度
 31 void dfs(int v,int p,int d)
 32 {
 33     parent[0][v]=p;
 34     depth[v]=d;
 35     for(int i=0;i<G[v].size();i++)
 36     {
 37         if(G[v][i]!=p)
 38             dfs(G[v][i],v,d+1);
 39     }
 40 }
 41 //预处理
 42 void init(int V)
 43 {
 44     //预处理出 parent[0]和depth
 45     dfs(root,-1,0);
 46     //预处理出parent
 47     for(int k=0;k+1<MAX_LOG_V;k++)
 48         for(int v=0;v<V;v++)
 49         {
 50             if(parent[k][v]<0)
 51                 parent[k+1][v]=-1;
 52             else
 53                 parent[k+1][v]=parent[k][parent[k][v]];
 54         }
 55 }
 56 //计算u和v的LCA
 57 int lca(int u,int v)
 58 {
 59     //让u和v向上走到同一深度
 60     if(depth[u]>depth[v])
 61         swap(u,v);
 62     for(int k=0;k<MAX_LOG_V;k++)
 63     {
 64         if(((depth[v]-depth[u])>>k)&1)
 65             v=parent[k][v];
 66     }
 67     if(u==v)
 68         return u;
 69     for(int k=MAX_LOG_V-1;k>=0;k--)
 70     {
 71         if(parent[k][u]!=parent[k][v])
 72         {
 73             u=parent[k][u];v=parent[k][v];
 74         }
 75     }
 76     return u==v?u:parent[0][u];
 77 }
 78 int dis(int x,int y)
 79 {
 80     if(depth[x]<depth[y])
 81         swap(x,y);
 82     int lo=lca(x,y);
 83     if(lo==y)
 84         return depth[x]-depth[y]+1;
 85     else
 86         return depth[x]+depth[y]-2*depth[lo]+1;
 87 }
 88 int n,q;
 89 int main()
 90 {
 91     scanf("%d%d",&n,&q);
 92     for(int i=1;i<n;i++)
 93     {
 94         int x;
 95         scanf("%d",&x);--x;
 96         G[x].push_back(i);
 97     }
 98     init(n);
 99     while(q--)
100     {
101         int a[3],b[3],c[3];
102         for(int i=0;i<3;i++)
103             scanf("%d",&a[i]);
104         for(int i=0;i<3;i++)
105             --a[i];
106         int an=-1,anz=0;
107         for(int i=0;i<3;i++)
108         {
109             int x=i,y=(i+2)%3;
110             b[i]=lca(a[x],a[y]);
111             if(depth[b[i]]>an)
112             {
113                 an=depth[b[i]];
114                 anz=b[i];
115             }
116         }
117         an=0;
118         for(int i=0;i<3;i++)
119             an=max(an,dis(a[i],anz));
120         printf("%d\n",an);
121     }
122 }

 

posted @ 2017-07-25 10:55  perplex  阅读(292)  评论(0编辑  收藏  举报