#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. 《Cracking the Coding Interview》——第2章:链表——题目2

    2014-03-18 02:24 题目:给定一个单链表,找出倒数第K个节点. 解法:让一个指针先走K步,然后俩指针一起走到尽头.当然也可以先走到尽头数出链表的长度,然后第二次少走K步.其实耗费的工夫是 ...

  2. python2.7写入文件时指定编码为utf-8

    python3.0可以这样写 f = open('ufile.log', 'w', 'utf-8')   但在python2.7中open()没有编码参数,如上那样写会报错,可以使用如下模块 impo ...

  3. Python全栈 MySQL 数据库(SQL命令大全、MySQL 、Python调用)

    为了梦想与了信仰    开局一张图   主要三个方面: 1.Linux终端命令 2.MySQL语句 3.Python调用   先删库 再跑路.....                         ...

  4. 从今天开始学Python

    外部链接下载吧 1.  Python 3.63.chm   AIP 帮助文档  下载:https://pan.baidu.com/s/1lhpv8JTC3Z7B6aZ3qQi40g 2.  VMwar ...

  5. 团队项目-第七次scrum 会议

    时间:11.3 时长:30分钟 地点:F楼1039教室 工作情况 团队成员 已完成任务 待完成任务 解小锐 学习cocos creator样例 修复员工招聘时bug 陈鑫 完成fire()与UI的对接 ...

  6. ajax是可以本地运行的

    ajax是可以本地运行的,经过验证,可以是可以,但跟浏览器有关,火狐和新IE可以,chrome不可以,旧ie不知道什么原因也不可以.但是浏览器也有它的安全策略,必须是同一目录下的文件可以访问.chro ...

  7. Angular & RxJS & Typesc­ript

    Angular & RxJS & Typesc­ript https://www.wmnetwork.cc/d/?mid=75627 杭州经开区国际创博中心 https://www.w ...

  8. ZooKeeper概述与安装

    ZooKeeper笔记 ZooKeeper概述 背景: 现代企业对计算机系统的计算存储能力要求越来越高,单纯的高性能服务器已经无法满足要求.企业的IT架构从集中式向分布式过度. 所谓分布式,就是将一个 ...

  9. 【转】oracle 删除重复记录

    转至:http://blog.163.com/aner_rui/blog/static/12131232820105901451809/ 2.保留一条(这个应该是大多数人所需要的 ^_^) Delet ...

  10. [poj] 2618 popular cows

    原题 这是一个强连通分量板子题. a thinks b is popular 即为a到b有一条边,要求被所有牛popular的牛的个数. 所求为对图进行强连通分量缩点后,没有出度的强连通分量里的点数( ...