【 Codeforces Round #425 (Div. 2) D】Misha, Grisha and Underground
【Link】:http://codeforces.com/contest/832/problem/D
【Description】
给你一棵树;
然后给你3个点
让你把这3个点和点s,t,f对应;
然后s先从s走到f;
之后t再从t走到f;
求这两条路径的公共路径的长度;
【Solution】
答案为
dis(s,f)+dis(t,f)−dis(s,t)2
树上最短路径做一下就好;
LCA!
【NumberOf WA】
0
【Reviw】
想得太慢了
【Code】
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 100000+5;
const int MAX = 17;
vector <int> son[MAXN],w[MAXN];
int n,p[MAXN][MAX+5],dep[MAXN],pre[MAX+5],m,t[3];
long long dis[MAXN];
void input(int &r)
{
char t = getchar();
while (!isdigit(t)) t = getchar();
r = 0;
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
}
void dfs(int x,int f)
{
dep[x] = dep[f] + 1;
p[x][0] = f;
for (int i = 1; i <= MAX; i++)
p[x][i] = p[p[x][i - 1]][i - 1];
int len = son[x].size();
for (int i = 0; i <= len - 1; i++)
{
int y = son[x][i];
if (y != f)
{
dis[y] = dis[x] + w[x][i];
dfs(y, x);
}
}
}
int midis(int t0,int t1){
int pret0,pret1;
pret0 = t0; pret1 = t1;
if (dep[t0] > dep[t1])
swap(t0, t1);
for (int i = MAX; i >= 0; i--)
if (dep[t0] <= dep[t1] - pre[i])
t1 = p[t1][i];
if (t1 == t0)
{
return dis[pret0]+dis[pret1]-2*dis[t0];
}
for (int i = MAX; i >= 0; i--)
{
if (p[t0][i] == p[t1][i])
continue;
t0 = p[t0][i], t1 = p[t1][i];
}
return dis[pret0]+dis[pret1]-2*dis[p[t0][0]];
}
int get_ans(){
int temp1 = 0;
for (int S = 0;S <= 2;S++)
for (int T = 0;T <= 2;T++)
if (S!=T)
for (int F = 0;F <= 2;F++)
if (S!=F && T!=F){
int temp = midis(t[S],t[F]);
temp+=midis(t[T],t[F]);
temp-=midis(t[S],t[T]);
temp/=2;
temp1 = max(temp1,temp);
}
return temp1;
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
pre[0] = 1;
for (int i = 1; i <= MAX; i++)
pre[i] = pre[i - 1] << 1;
input(n); input(m);
for (int i = 2; i <= n; i++)
{
int x;
input(x);
son[x].push_back(i);
w[x].push_back(1);
}
dis[1] = 0;
dfs(1, 0);
for (int i = 1; i <= m; i++)
{
int t0, t1,t2,pret0,pret1;
for (int j = 0;j <= 2;j++)
input(t[j]);
printf("%d\n",get_ans()+1);
}
return 0;
}
【 Codeforces Round #425 (Div. 2) D】Misha, Grisha and Underground的更多相关文章
- Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组
Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...
- 【Codeforces Round #425 (Div. 2) B】Petya and Exam
[Link]:http://codeforces.com/contest/832/problem/B [Description] *能代替一个字符串(由坏字母组成); ?能代替单个字符(由好字母组成) ...
- 【Codeforces Round #425 (Div. 2) A】Sasha and Sticks
[Link]: [Description] [Solution] 傻逼题; 获取n/k; 对n/k的奇偶性讨论一下就好 [NumberOf WA] 0 [Reviw] [Code] #include ...
- 【Codeforces Round #432 (Div. 1) B】Arpa and a list of numbers
[链接]h在这里写链接 [题意] 定义bad list是一个非空的.最大公约数为1的序列.给定一个序列,有两种操作:花费x将一个元素删除.花费y将一个元素加1,问你将这个序列变为good list所需 ...
- 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes
[题目链接]:http://codeforces.com/contest/821/problem/C [题意] 给你2*n个操作; 包括把1..n中的某一个数压入栈顶,以及把栈顶元素弹出; 保证压入和 ...
- 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees
[题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...
- 【Codeforces Round #420 (Div. 2) A】Okabe and Future Gadget Laboratory
[题目链接]:http://codeforces.com/contest/821/problem/A [题意] 给你一个n*n的数组; 然后问你,是不是每个位置(x,y); 都能找到一个同一行的元素q ...
- 【Codeforces Round #423 (Div. 2) C】String Reconstruction
[Link]:http://codeforces.com/contest/828/problem/C [Description] 让你猜一个字符串原来是什么; 你知道这个字符串的n个子串; 且知道第i ...
- 【Codeforces Round #423 (Div. 2) B】Black Square
[Link]:http://codeforces.com/contest/828/problem/B [Description] 给你一个n*m的格子; 里面包含B和W两种颜色的格子; 让你在这个格子 ...
随机推荐
- Centos7+httpd+fastcgi+rails安装
搭建的环境: centos7 Apache/2.4.6 fastcgi2.4.6 rails4 在安装fastcgi的时候遇到了问题: 问题: .... .. In file included fro ...
- 汇编中中括号[]作用以及lea和mov指令的区别
现在总结一下:其中牵扯到lea指令,mov指令,[] 一.lea指令:对于寄存器来说:第二个操作数是寄存器必须要加[],不然报错,这里lea就是取[寄存器]的值,如:mov eax,2lea ebx, ...
- List methods
Python provides methods that operate on lists. For example, append adds a new element to the end of ...
- The python programing language
Python is an example of high-level language. As you might infer from the name “high-level language”, ...
- [-] Failed to load plugin from /usr/share/metasploit-framework/plugins/db_autopwn: No classes were loaded from /usr/share/metasploit-framework/plugins/db_autopwn in the Msf::Plugin namespace.
问题详情 然后,执行,出现如下问题,则说明大家的这个文件,下载不是完整的或者你上传不完整. msf > load db_autopwn [-] Failed to load plugin fro ...
- PostgreSQL Replication之第九章 与pgpool一起工作(1)
在前面的章节中,我们已经能够深入地理解了pgbouncer,同时也学会了如何使用它来尽可能地优化复制设置.在本章我们将了解一个经常被称作与pgbouncer相对应的工具.尽管pgpool的思想与pgb ...
- PostgreSQL Replication之第六章 监控您的设置(3)
6.3 检查操作系统进程 一旦我们检查了归档以及我们的系统视图,我们就准备检查系统 进程.检查系统进程可能看起来有点粗糙,但它被证明非常有效. 在master上,我们可以简单地检查一个名为wal_se ...
- EasyUI 之 DataGrid利用用拉姆达表达式实现分页查询
首先,我们在DataGrid的URL中加上我们要查询的条件:查询用户名不是“呵呵”的所有用户. <div> <table id="dg" class=&quo ...
- .net Web获取域用户账号
HttpContext.Current.Request.LogonUserIdentity.Name //可以获取出域账号 HttpContext.Current.Request.LogonUserI ...
- vue下载模板、导出excle
1.下载模板是 下载模版比较简单,就是跳一个新的页面 2.导出excle 就是get请求,把自己要导出的参数一一拼接起来 }