Codeforces Round #425 (Div. 2) Misha, Grisha and Underground(LCA)
2 seconds
256 megabytes
standard input
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.
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 2
1 1
1 2 3
2 3 3
2
3
4 1
1 2 3
1 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.
【题意】给你一棵树,然后q次询问,每次给出三个点a,b,c,要求从其中两个点走到第三个点,使得两种路径中共同经过的点最多,最多是多少?
【分析】枚举终点,然后判一下就行了。
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 2e5+;
const int M = ;
const int mod = 1e9+;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,m,ans;
int dep[N],fa[N][];
vector<int>edg[N];
void dfs(int u,int f){
fa[u][]=f;
for(int i=;i<;i++){
fa[u][i]=fa[fa[u][i-]][i-];
}
for(int v : edg[u]){
if(v==f)continue;
dep[v]=dep[u]+;
dfs(v,u);
}
}
int LCA(int u,int v){
int U=u,V=v;
if(dep[u]<dep[v])swap(u,v);
for(int i=;i>=;i--){
if(dep[fa[u][i]]>=dep[v]){
u=fa[u][i];
}
}
if(u==v)return (u);
for(int i=;i>=;i--){
if(fa[u][i]!=fa[v][i]){
u=fa[u][i];v=fa[v][i];
}
}
return (fa[u][]);
}
void solve(int u,int v,int t){
int ut=LCA(u,t);
int vt=LCA(v,t);
if(dep[ut]<dep[t]&&dep[vt]<dep[t]){
if(ut==vt){
int uv=LCA(u,v);
ans=max(ans,dep[t]-max(dep[ut],dep[vt])++dep[uv]-dep[ut]);
}
else ans=max(ans,dep[t]-max(dep[ut],dep[vt])+);
}
else if(ut==t&&vt==t){
int uv=LCA(u,v);
if(uv!=t)ans=max(ans,dep[uv]-dep[t]+);
else ans=max(ans,);
}
else ans=max(ans,);
}
int main(){
scanf("%d%d",&n,&m);
for (int i=,v;i<=n;i++){
scanf("%d",&v);
edg[i].pb(v);
edg[v].pb(i);
}
dep[]=;
dfs(,);
while(m--){
int a,b,c;
ans=;
scanf("%d%d%d",&a,&b,&c);
solve(a,b,c);
solve(a,c,b);
solve(b,c,a);
printf("%d\n",ans);
}
return ;
}
Codeforces Round #425 (Div. 2) Misha, Grisha and Underground(LCA)的更多相关文章
- Codeforces Round #184 (Div. 2) E. Playing with String(博弈)
题目大意 两个人轮流在一个字符串上删掉一个字符,没有字符可删的人输掉游戏 删字符的规则如下: 1. 每次从一个字符串中选取一个字符,它是一个长度至少为 3 的奇回文串的中心 2. 删掉该字符,同时,他 ...
- Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)
Problem Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 1000 mSec Problem Descripti ...
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(分形)
E. Dasha and Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #267 (Div. 2) C. George and Job(DP)补题
Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...
- Codeforces Round #394 (Div. 2) C.Dasha and Password(暴力)
http://codeforces.com/contest/761/problem/C 题意:给出n个串,每个串的初始光标都位于0(列)处,怎样移动光标能够在凑出密码(每个串的光标位置表示一个密码的字 ...
- Codeforces Round #107 (Div. 1) B. Quantity of Strings(推算)
http://codeforces.com/problemset/problem/150/B 题意: 给出n,m,k,n表示字符串的长度为n,m表示字符种类个数,k表示每k个数都必须是回文串,求满足要 ...
- Codeforces Round #332 (Div. 2) D. Spongebob and Squares(枚举)
http://codeforces.com/problemset/problem/599/D 题意:给出一个数x,问你有多少个n*m的网格中有x个正方形,输出n和m的值. 思路: 易得公式为:$\su ...
- Codeforces Round #419 (Div. 2) A. Karen and Morning(模拟)
http://codeforces.com/contest/816/problem/A 题意: 给出一个时间,问最少过多少时间后是回文串. 思路: 模拟,先把小时的逆串计算出来: ① 如果逆串=分钟, ...
- Codeforces Round #390 (Div. 2) C. Vladik and chat(dp)
http://codeforces.com/contest/754/problem/C C. Vladik and chat time limit per test 2 seconds memory ...
随机推荐
- mvc Dapper_Report_Down_ExcelFile
一.基于Aspose.Cells.Dapper导出Excel Dapper的Query返回要不是对象的IEnumerable,要不是Dynamic的IEnumerable,都不适合不用反射就能够动态获 ...
- 知问前端——Ajax表单插件
传统的表单提交,需要多次跳转页面,极大的消耗资源也缺乏良好的用户体验.而这款form.js表单的Ajax提交插件将解决这个问题. 一.核心方法 官方网站:http://malsup.com/jquer ...
- java将文件转为UTF8工具类
package hiveTest; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File ...
- Oracle数据库,忽略大小写Like模糊查询(SQL Server,MySql原理相同)
背景 在使用Oracle或者其它数据库时,使用like 关键字进行模糊查询是大家经常使用的功能,在纯中文环境中使用非常好用,还有一些通配符可以使用,但是在纯英文环境中,会出现大小需要精确匹配的问题,主 ...
- Why to Not Not Start a Startup
我花了周六,周日两天的时间,把这篇长文给阅读完了.很受益,改变了我的很多认知,也给我开拓了视野. 转载: Want to start a startup? Get funded by Y Combin ...
- JS之document例题讲解2
例题三.图片轮播 <body> <div style="width:1000px; height:250px; margin-top:30px"> < ...
- canvas_基于canvan绘制的双半圆环进度条
效果图 实现原理: 1.使用canvas绘制两个半圆弧,底图灰色半圆弧和颜色进度圆弧. 2.利用setInterval计时器,逐步改变颜色进度条,达到进度条的效果. 效果代码: <!DOCTYP ...
- tornado 学习之GET POST方法 -- (转)
import torndb import tornado.web import tornado.ioloop from tornado.options import define,options,pa ...
- 破解邻居家的wifi密码
刚刚学习了如何破解wifi密码 然后昨天晚上连续破解了两个 好激动 我是在ubuntu上面使用aircrack-ng套件进行破解的 首先进行抓包,然后跑字典就ok了 下面的不错 一.关闭网络和结束可能 ...
- shellcheck 帮助你写出更好的脚本
简介 shellcheck 是一款实用的 shell脚本静态检查工具. 首先,可以帮助你提前发现并修复简单的语法错误,节约时间.每次都需要运行才发现写错了一个小地方,确实非常浪费时间. 其次,可以针对 ...