Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground LCA
来源:互联网 发布:mac重装没有磁盘 编辑:程序博客网 时间:2024/06/06 05:28
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.
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.
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.
3 21 11 2 32 3 3
23
4 11 2 31 2 3
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.
一棵树,给三个节点,求它们间一个点到其余两个点路径的公共路径上经过节点数的最大值
LCA.
把两两之间的LCA求出来,设起点为A,终点为B、C,公共路径的距离即Vab+Vac-Vbc,两点之间的距离可由LCA求出。由于需要求的是节点数量,最终的结果需要+1.
#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <bitset>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;const int maxn=100005,inf=0x3f3f3f3f;const ll llinf=0x3f3f3f3f3f3f3f3f; const ld pi=acos(-1.0L);int head[maxn],a[maxn*2],mind[maxn*2][20],f[maxn],d[maxn],dep[maxn*2];bool visit[maxn];int dfn,num;struct Edge {int from,to,pre,dist;};Edge edge[maxn*2];void addedge(int from,int to) {edge[num]=(Edge){from,to,head[from]};head[from]=num++;edge[num]=(Edge){to,from,head[to]};head[to]=num++;}void dfs(int now,int step) {visit[now]=1;a[++dfn]=now;f[now]=dfn;dep[dfn]=step;for (int i=head[now];i!=-1;i=edge[i].pre) {int to=edge[i].to;if (!visit[to]) {d[to]=d[now]+1;dfs(to,step+1);a[++dfn]=now;dep[dfn]=step;}}}void init(int n) {int i,j;for (i=1;i<=n;i++) mind[i][0]=i;for (j=1;(1<<j)<=n;j++) {for (i=1;i+(1<<j)-1<=n;i++) {int p=mind[i][j-1],q=mind[i+(1<<(j-1))][j-1];mind[i][j]=dep[p]<dep[q]?p:q;}}}int find(int l,int r) {int k=0;if (l>r) {int p=l;l=r;r=p;}while (1<<(k+1)<=(r-l+1)) k++;int p=mind[l][k],q=mind[r-(1<<k)+1][k];return dep[p]<dep[q]?a[p]:a[q];}int main() {memset(head,-1,sizeof(head));int n,q,i,j,x,y,z;scanf("%d%d",&n,&q);num=0;for (i=1;i<n;i++) {scanf("%d",&x);addedge(x,i+1);}mem0(visit);d[1]=dfn=0;dfs(1,0);init(dfn);for (i=1;i<=q;i++) {scanf("%d%d%d",&x,&y,&z);int pxy,pyz,pxz,dxy,dyz,dxz;pxz=find(f[x],f[z]);pyz=find(f[y],f[z]);pxy=find(f[x],f[y]);dxy=d[x]+d[y]-2*d[pxy];dyz=d[z]+d[y]-2*d[pyz];dxz=d[z]+d[x]-2*d[pxz];int ans=max(max(dxy+dyz-dxz,dyz+dxz-dxy),dxy+dxz-dyz);printf("%d\n",ans/2+1);}return 0;}
- 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) D. Misha, Grisha and Underground
- 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)D. Misha, Grisha and Underground(LCA)
- codeforces D. Misha, Grisha and Underground(LCA)
- Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground 最近公共祖先
- CodeForces 832D Round #425 D Misha, Grisha and Underground :LCA求树上路径长度
- 【Lca 倍增】codeforces 832D Misha, Grisha and Underground
- Codeforces 832D-Misha, Grisha and Underground(LCA)
- Codeforces 832D. Misha, Grisha and Underground【LCA】
- Codeforces 832D Misha, Grisha and Underground【LCA】
- codeforces 832D Misha, Grisha and Underground 倍增lca
- CF #425 DIV2 D. Misha, Grisha and Underground(LCA)
- codeforces 832D Misha, Grisha and Underground
- Codeforces 832D Misha, Grisha and Underground
- [codeforces 832D]Misha, Grisha and Underground
- Linux 管理网络
- Linux常用命令
- 今天是上班的第三天,以后的路还很长,记录一下个人的成长,本人屌丝程序猿一个
- 杭电1008坐电梯
- SQL经典题目
- Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground LCA
- HDU
- 2017IMO第五题
- 循环
- HDU 6038 Function (多校1)
- Hdu图论最短路基础题
- VS工程文件常见后缀名文件含义
- 双系统下Ubuntu16.04与Windows10时间不同步解决办法
- nginx反向代理和负载均衡