#425 Div2 D

题意

给出一个树形图,每次询问给出三个点,从其中选择两个作为起始点,一个终点,求从两个起始点出发(走最短路)到达终点经过的共同的点最多的数量。

分析

这种树上点与点之间距离有关的问题大多与 LCA 有关,那么我暴力枚举每个点分别作为起始点、终点,求下最大距离就好了。

code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10;
const int LOG_N = 18;
int n;
int dep[MAXN];
int par[MAXN][20];
vector<int> G[MAXN];
void dfs(int fa, int u, int d) {
par[u][0] = fa;
dep[u] = d;
for(int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if(v != fa) {
dfs(u,v, d + 1);
}
}
}
void init() {
for(int i = 1; (1 << i) < MAXN; i++) {
for(int j = 1; j <= n; j++) {
if(par[j][i - 1] == -1) par[j][i - 1] = -1;
else par[j][i] = par[par[j][i - 1]][i - 1];
}
}
}
int lca(int u, int v) {
if(dep[u] > dep[v]) swap(u, v);
for(int i = 0; (1 << i) < MAXN; i++) {
if((dep[v] - dep[u]) >> i & 1) {
v = par[v][i];
}
}
if(v == u) return u;
for(int i = LOG_N; i >= 0; i--) {
if(par[u][i] != par[v][i]) {
u = par[u][i];
v = par[v][i];
}
}
return par[u][0];
}
int query(int u, int v) {
int w = lca(u, v);
return dep[u] + dep[v] - 2 * dep[w];
}
int main() {
int q;
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
for(int i = 2; i <= n; i++) {
int x;
cin >> x;
G[i].push_back(x);
G[x].push_back(i);
}
dfs(-1, 1, 0);
init();
while(q--) {
int a[3];
for(int i = 0; i < 3; i++) cin >> a[i];
int ans = 0;
sort(a, a + 3);
do {
int s = a[0], t = a[1], f = a[2];
if(s == t) ans = max(ans, query(s, f));
else {
int w = lca(s, t), w1 = lca(s, f), w2 = lca(t, f);
if(dep[w1] > dep[w2]) { swap(s, t); swap(w1, w2); }
if(lca(f, w1) == lca(w1, w2)) { // f w1 w2 在一条链上
ans = max(ans, query(w2, f));
}
if(w1 == w2) {
ans = max(ans, query(w, f));
}
}
}while(next_permutation(a, a + 3));
cout << ans + 1 << endl;
}
return 0;
}

Codeforces #425 Div2 D的更多相关文章

  1. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  2. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  3. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  4. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  5. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  6. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  7. codeforces round 425 div2

    A. Sasha and Sticks 水题,判断一下次数的奇和偶就可以的. B. Petya and Exam 赛上的时候没有写出来,orz,记录一下吧. 题意:给出一个模式串,可能会有?和*两种符 ...

  8. Codeforces #263 div2 解题报告

    比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...

  9. codeforces #round363 div2.C-Vacations (DP)

    题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...

随机推荐

  1. Python 绘制棋盘

    import turtle pen = turtle.Pen() pen.speed(10) width = 30 # 格子宽度 count = 18 # 横向纵向格子数 o = width * co ...

  2. python 读取数据库中文内容显示一堆问号

    需要在连接数据库时 设置编码格式 def select_db(self,db_name): self.conn = MySQLdb.connect( host = self.ip, port = se ...

  3. httpclient传参类型与响应参数接收

    https://blog.csdn.net/qq_26562641/article/details/72817457 https://blog.csdn.net/chenjf0221/article/ ...

  4. 孤荷凌寒自学python第二十一天初识python的类

    孤荷凌寒自学python第二十一天初识python的类 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 类是面向对象的编程语言非常重要的概念. 编程语言的进化史中从顺序编程到结构化编程,最后才 ...

  5. 孤荷凌寒自学python第十五天python循环控制语句

    孤荷凌寒自学python第十五天python循环控制语句 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) python中只有两种循环控制语句 一.while循环 while 条件判断式 1: ...

  6. Unresolved defparam reference to 'read_aclr_synch' in dcfifo_component.read_aclr_synch

    问题: 今天用 questasim 仿真出现下面这个问题. Unresolved defparam reference to 'read_aclr_synch' in dcfifo_component ...

  7. python中os.path.join和join的区别

    这两个函数都是python的系统函数,都有“组合”.“连接”之意,但用法和应用场景千差万别 函数说明: 1.join函数 用法:用于连接字符串数组.将字符串.元组.列表中的元素以指定的字符(即分隔符) ...

  8. 【转】Unity3D研究院之设置自动旋转屏幕默认旋转方向

    http://www.xuanyusong.com/archives/2871 如下图所示,在处理屏幕默认旋转方向的时候可以在这里进行选择,上下左右一共是4个方向. 策划的需求是游戏采用横屏,但是要求 ...

  9. 如何修改root密码

    默认情况下,每次登录ubuntu都会生成一个随机的root密码,如果想要修改, sudo passwd 然后输入密码,这个密码就作为root用户的密码

  10. JAVA判断一个字符串里面有没有汉字

    private static boolean checkIfExistChineseCharacter(String s) { return !(s.length() == s.getBytes(). ...