CF832D:Misha, Grisha and Underground(LCA)
2017-07-25 21:09
573 查看
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 sto
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 sand 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
output
input
output
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.
题意:一棵树,q个询问,每个询问给三个点,问哪两个点到另外一个点的路径重合点数最多。
思路:在线LCA,分类讨论。题解做法:
# include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+3;
vector<int>v[maxn];
int h[maxn], fa[maxn][20];
void dfs(int cur, int pre)
{
fa[cur][0] = pre;
for(int i=1; i<20; ++i)
fa[cur][i] = fa[fa[cur][i-1]][i-1];
for(auto to : v[cur])
{
if(to == pre) continue;
h[to] = h[cur] + 1;
dfs(to, cur);
}
}
int lca(int v, int u)
{
if(h[v] > h[u]) swap(v, u);
for(int i=0; i<20; ++i)
if(h[u]-h[v]>>i & 1)
u = fa[u][i];
for(int i=19; i>=0; --i)
if(fa[v][i] != fa[u][i])
v = fa[v][i], u=fa[u][i];
return v == u?v:fa[v][0];
}
int cal(int s, int t, int f)
{
int ans=0, sf = lca(s, f)==f, tf = lca(t, f)==f;
if(sf != tf) return 1;//一个lca为f一个不是,返回1.
else if(sf) ans = h[lca(s, t)] - h[f];//两个都是。
else if(lca(s, f) != lca(t, f)) ans = h[f] - max(h[lca(f,s)], h[lca(f, t)]);//(s,e在不同支,f在他们其中一支)。
else ans = h[lca(s, t)] + h[f] - 2*h[lca(s, f)];//(s,e在同一支,f在另一支)
return ans + 1;
}
int main()
{
int n, q;
scanf("%d%d",&n,&q);
for(int i=2; i<=n; ++i)
{
int x;
scanf("%d",&x);
v[i].push_back(x);
v[x].push_back(i);
}
dfs(1, 0);
while(q--)
{
int a, b, c;
scanf("%d%d%d",&a,&b,&c);
printf("%d\n",max(cal(a, b, c), max(cal(b, c, a), cal(c, a, b))));
}
return 0;
}
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 sto
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 sand 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.
题意:一棵树,q个询问,每个询问给三个点,问哪两个点到另外一个点的路径重合点数最多。
思路:在线LCA,分类讨论。题解做法:
# include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+3;
vector<int>v[maxn];
int h[maxn], fa[maxn][20];
void dfs(int cur, int pre)
{
fa[cur][0] = pre;
for(int i=1; i<20; ++i)
fa[cur][i] = fa[fa[cur][i-1]][i-1];
for(auto to : v[cur])
{
if(to == pre) continue;
h[to] = h[cur] + 1;
dfs(to, cur);
}
}
int lca(int v, int u)
{
if(h[v] > h[u]) swap(v, u);
for(int i=0; i<20; ++i)
if(h[u]-h[v]>>i & 1)
u = fa[u][i];
for(int i=19; i>=0; --i)
if(fa[v][i] != fa[u][i])
v = fa[v][i], u=fa[u][i];
return v == u?v:fa[v][0];
}
int cal(int s, int t, int f)
{
int ans=0, sf = lca(s, f)==f, tf = lca(t, f)==f;
if(sf != tf) return 1;//一个lca为f一个不是,返回1.
else if(sf) ans = h[lca(s, t)] - h[f];//两个都是。
else if(lca(s, f) != lca(t, f)) ans = h[f] - max(h[lca(f,s)], h[lca(f, t)]);//(s,e在不同支,f在他们其中一支)。
else ans = h[lca(s, t)] + h[f] - 2*h[lca(s, f)];//(s,e在同一支,f在另一支)
return ans + 1;
}
int main()
{
int n, q;
scanf("%d%d",&n,&q);
for(int i=2; i<=n; ++i)
{
int x;
scanf("%d",&x);
v[i].push_back(x);
v[x].push_back(i);
}
dfs(1, 0);
while(q--)
{
int a, b, c;
scanf("%d%d%d",&a,&b,&c);
printf("%d\n",max(cal(a, b, c), max(cal(b, c, a), cal(c, a, b))));
}
return 0;
}
相关文章推荐
- 【树链剖分】【dfs序】【LCA】【分类讨论】Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground
- (LCA)Codeforces Round #425 (Div. 2) D - Misha, Grisha and Underground
- Codeforces 832D-Misha, Grisha and Underground(LCA)
- CodeForces 832D Round #425 D Misha, Grisha and Underground :LCA求树上路径长度
- Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground LCA
- Codeforces Round #425 (Div. 2)D. Misha, Grisha and Underground(LCA)
- Codeforces Round #425 (Div. 2) Misha, Grisha and Underground(LCA)
- Codeforces 832D: Misha, Grisha and Underground 【LCA模板】
- codeforces D. Misha, Grisha and Underground(LCA)
- D. Misha, Grisha and Underground(LCA 倍增)
- Codeforces Round #425 (Div. 2)D. Misha, Grisha and Underground
- cf 832D Misha, Grisha and Underground 【lca+倍增】
- Codeforces Round #425 (Div. 2) D.Misha, Grisha and Underground
- codeforces 832 D Misha, Grisha and Underground(倍增)
- codeforces 832D——Misha, Grisha and Underground(LCA)
- Codeforces Round #425 (Div. 2) D.Misha, Grisha and Underground
- [刷题]Codeforces Round #425(Div. 2) - D. Misha, Grisha and Underground
- Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组
- 【Lca 倍增】codeforces 832D Misha, Grisha and Underground
- [codeforces 832D]Misha, Grisha and Underground