Codeforces #425 Div2 D
#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的更多相关文章
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- codeforces round 425 div2
A. Sasha and Sticks 水题,判断一下次数的奇和偶就可以的. B. Petya and Exam 赛上的时候没有写出来,orz,记录一下吧. 题意:给出一个模式串,可能会有?和*两种符 ...
- Codeforces #263 div2 解题报告
比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...
- codeforces #round363 div2.C-Vacations (DP)
题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...
随机推荐
- 《Cracking the Coding Interview》——第4章:树和图——题目9
2014-03-19 05:07 题目:给定一棵二叉树T和一个值value,在T中找出所有加起来和等于value的路径.路径的起点和终点都可以是树的任意节点. 解法:我偷了个懒,直接把这棵树看成一个无 ...
- rownum浅谈(二)
上篇说到rownum和order by及索引列的关系,明白了通过构建一个子查询把查询结果固定住再取数就可以了 .还是取最近10条创建的用户: select * from (select u.* fro ...
- shell sort 排序大讨论
转自http://roclinux.cn 本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. === [正文开始]有时候学 ...
- JSP/Servlet Web 学习笔记 DayFour
Servlet概述 Servelt是使用Java Servlet应用程序接口及相关类和方法的Java程序. Servlet是用Java编写的Server端程序,它与协议和平台无关.Servlet运行于 ...
- 第二阶段团队冲刺-four
昨天: 绘制logo. 今天: 用servlet完成名单打印功能. 遇到的问题: servlet中的获得的路径不是想要的.
- HTTP 返回状态代码详细解释
一:1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态代码. 100(继续)请求者应当继续提出请求. 服务器返回此代码表示已收到请求的第一部分,正在等待其余部分:101(切换协议)请求者已要 ...
- Access连接字符串
Access2007没有密码连接: <connectionStrings> <add name="myconn" connectionString="P ...
- hadoop2.6.4【windows7】构建maven项目 系列2
准备windows版本的hadoop2.6.4 下载windows版本的hadoop2.6.4解压在本地 新建maven项目构建hadoop依赖 <?xml version="1.0& ...
- thinkphp3.2 学习笔记 基础篇
环境要求:PHP5.3以上版本注意:PHP5.3DEV和php6不支持 目录结构 www WEB部署目录(或者子目录)├─index.php 入口文件├─README.md README文件├─App ...
- HTTPS和HTTP的区别:
https协议需要到ca申请证书,一般免费证书很少,需要交费.http是超文本传输协议,信息是明文传输,https 则是具有安全性的ssl加密传输协议http和https使用的是完全不同的连接方式用的 ...