【 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两种颜色的格子; 让你在这个格子 ...
随机推荐
- poj2280--Amphiphilic Carbon Molecules(扫描线+极角排序+转换坐标)
题目链接:id=2280">点击打开链接 题目大意:给出n个点的坐标.每一个点有一个值0或者1,如今有一个隔板(无限长)去分开着n个点,一側统计0的个数,一側统计1的个数,假设点在板上 ...
- elasticsearch中的几个概念总结
1.Geo spatial search : 地理空间搜索,可以在搜索查询中指定的某一距离内查找所要的内容.也可以返回以当前为圆心,逐渐添加圆的半径.直到找到所匹配到的内容. 參考:http://ww ...
- Light OJ 1080 - Binary Simulation
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1080 1080 - Binary Simulation PDF (Englis ...
- JAVA学习第四十五课 — 其它对象API(一)System、Runtime、Math类
一.System类 1. static long currentTimeMillis() 返回以毫秒为单位的当前时间. 实际上:当前时间与协调世界时 1970 年 1 月 1 日午夜之间的时间差(以毫 ...
- zzulioj--1832--贪吃的松鼠(位运算好题)
1832: 贪吃的松鼠 Time Limit: 3 Sec Memory Limit: 2 MB Submit: 43 Solved: 7 SubmitStatusWeb Board Descri ...
- P2264 情书(字符串hash90分)
题目背景 一封好的情书需要撰写人全身心的投入.lin_toto同学看上了可爱的卡速米想对她表白,但却不知道自己写的情书是否能感动她,现在他带着情书请你来帮助他. 题目描述 为了帮助lin_toto,我 ...
- windows如何批量添加路由表
我大约有2000条路由,需要批量导入,如何才能快速导入,快速删除呢.如果直接用命令添加路由表的话感觉很慢. windows如何批量添加路由表 >> csharp这个答案描述的挺清楚的:ht ...
- mybatis如何成功插入后获取自增长的id
使用mybatis向数据库中插入一条记录,如何获取成功插入记录的自增长id呢? 需要向xml配置中加上一下两个配置: <insert id="add" useGenerate ...
- 最短路径—Dijkstra 算法和Floyd 算法
某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让行人很困扰. 现在 ...
- vue使用,问题
参考链接:https://cn.vuejs.org/v2/guide/index.html *)[Vue warn]: Error in v-on handler: "TypeError: ...