Codeforces 832D Misha, Grisha and Underground【LCA】
来源:互联网 发布:淘宝客服自我评价 编辑:程序博客网 时间:2024/06/06 05:44
Misha and Grisha are funny boys, so they like to use new underground. The underground hasn 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 stations 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 (includings and f). After that on the same day at evening Grisha will ride from stationt 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 stations on that day, another should be station f, and the remaining should be station t. They became interested how they should choose these stationss, 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 andq (2 ≤ n ≤ 105,1 ≤ q ≤ 105) — the number of stations and the number of days.
The second line contains n - 1 integersp2, p3, ..., pn (1 ≤ pi ≤ n). The integer pi means that there is a route between stationspi andi. It is guaranteed that it's possible to reach every station from any other.
The next q lines contains three integersa, b andc 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 stationss, t andf are chosen optimally from the three stations on thei-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 route1 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 route1 2 3, and Grisha would go on the route 2 3 and would see the text at both stations.
题目大意:
给你一棵树,每组查询三个点a,b,c,让你从中选取一个点作为终点,另外两个点作为起点,我们要求两个起点到终点的路径上,重复经过的点的个数最多,问这个最多点数。
思路:
设定S1,S2表示两个起点,T表示终点的话,如果我们能够知道S1->T S2->T的两条路径上重复覆盖的长度的话(LEN),我们就能够知道结果的点数(LEN+1)。
那么考虑求两条路径上的重复长度。
我们知道,求树上两点间距离需要求两点LCA,那么我们预处理倍增然后在线查询就行。
那么我们可以求出从S1到T的路径长度,也能求出S2到T的路径长度,那么求和后,再减去S1到LCA(S1,S2)的路径长度,再减去S2到LCA(S1,S2)的路径长度之后,得到的长度就是覆盖的重复路径长度*2,最终结果也就是这个结果/2+1。
Ac代码:
#include<stdio.h>#include<string.h>#include<vector>#include<math.h>using namespace std;#define N 100050struct node{ int from; int to; int w; int next;}e[350000];int cont,n,m;int p[150000][20];int d[150000];int dist[150000];int head[150000];void add(int from,int to,int w){ e[cont].to=to; e[cont].w=w; e[cont].next=head[from]; head[from]=cont++;}void Dfs(int u,int from){ for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].to; if(v==from)continue; d[v]=d[u]+1; dist[v]=dist[u]+1; p[v][0]=u; Dfs(v,u); }}void init(){ for(int j=1;(1<<j)<=n;j++) { for(int i=1;i<=n;i++) { p[i][j]=p[p[i][j-1]][j-1]; } }}int Lca(int x,int y){ if(d[x]>d[y])swap(x,y); int f=d[y]-d[x]; for(int i=0;(1<<i)<=f;i++) { if((1<<i)&f)y=p[y][i]; } if(x!=y) { for(int i=(int)log2(N);i>=0;i--) { if(p[x][i]!=p[y][i]) { x=p[x][i]; y=p[y][i]; } } x=p[x][0]; } return x;}int Dist(int a,int b){ return dist[a]+dist[b]-2*dist[Lca(a,b)];}int main(){ while(~scanf("%d%d",&n,&m)) { cont=0; memset(d,0,sizeof(d)); memset(dist,0,sizeof(dist)); memset(head,-1,sizeof(head)); for(int i=2;i<=n;i++) { int x;scanf("%d",&x); add(x,i,1); add(i,x,1); } Dfs(1,-1); init(); while(m--) { int a,b,c; scanf("%d%d%d",&a,&b,&c); int ans=0; ans=max(Dist(b,a)+Dist(c,a)-Dist(b,Lca(b,c))-Dist(c,Lca(b,c)),ans); ans=max(Dist(a,b)+Dist(c,b)-Dist(a,Lca(a,c))-Dist(c,Lca(a,c)),ans); ans=max(Dist(a,c)+Dist(b,c)-Dist(a,Lca(a,b))-Dist(b,Lca(a,b)),ans); printf("%d\n",ans/2+1); } }}
- codeforces 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
- codeforces 832D Misha, Grisha and Underground
- Codeforces 832D Misha, Grisha and Underground
- [codeforces 832D]Misha, Grisha and Underground
- 【Codeforces】832D. Misha, Grisha and Underground
- Codeforces-832D-Misha, Grisha and Underground
- CodeForces 832D Round #425 D Misha, Grisha and Underground :LCA求树上路径长度
- CF 832D Misha, Grisha and Underground(Tree+lca)
- cf 832D Misha, Grisha and Underground 【lca+倍增】
- CF --- 832D Misha, Grisha and Underground 【LCA + 思维】
- codeforces 832D——Misha, Grisha and Underground(LCA)
- Codeforces 832 D. Misha, Grisha and Underground 倍增法求LCA
- codeforces 832 D Misha, Grisha and Underground(倍增)
- 【云计算的1024种玩法】为求职加分:为自己建个炫酷的简历网页
- 网易笔试题 Fibobacci数列
- Java中对List集合内的元素进行顺序、倒序、随机排序的示例代码
- LightOJ-1007-Mathematically Hard [欧拉函数]
- DB2-SQLSTATE,DB2-SQLCODE
- Codeforces 832D Misha, Grisha and Underground【LCA】
- Hibernate3升级至Hibernate4.3报“数字溢出”
- Java_Date
- fopen 和 fopen_s
- 七夕,单身狗有话要说....
- VBO
- JavaScript对象遍历
- 企业仓库管理软件免费版要把处理好三个问题 利用凌云仓库管理软件免费版的智能补货功能可以快速的指定采购策略,这里面就需要及时了解每一张采购订单的执行情况,不但要防止遗漏订单情况的发生,更要防止情绪化采购
- ubuntu运行supervisor时报错,找不到supervisor.sock