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 ...
随机推荐
- 如何自己编译apue.3e中代码 & 学习写makefile
本来是搜pthread的相关资料,看blog发现很多linux程序员都看的一本神书<APUE>,里面有系统的两章内容专门讲pthread(不过是用c语言做的代码示例,这个不碍事,还是归到原 ...
- express常用代码片段
请求模块: var express = require('express'); var router = express.Router(); // 拿到express框架的路由 var mongoos ...
- springboot04 Ajax json Jquery
一.Ajax 1.同步&异步请求 在所有的请求响应交互世界里,我们有通常会划分出来两种形态的请求, 一种是同步请求.另一种是异步请求 .比如注册.登录.添加数据等等这些请求执行的就是同步请求, ...
- BZOJ 2186 沙拉公主的困惑
2186: [Sdoi2008]沙拉公主的困惑 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 3397 Solved: 1164 [Submit] ...
- CodeForces Round #515 Div.3 D. Boxes Packing
http://codeforces.com/contest/1066/problem/D Maksim has nn objects and mm boxes, each box has size e ...
- 发现一个form小问题
在使用编辑器及框架时,form表单如果在太靠内的div层里,就取不到textarea的post值,具体原因位置,可能跟框架的CSS有关
- 【bzoj1856】[Scoi2010]字符串 Catalan数
题目描述 lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数不能少于0的个数.现在lxhgww想要知道满足 ...
- POJ 1273 Drainage Ditches | 最大流模板
#include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...
- POJ 2749 Building roads 2-sat+二分答案
把爱恨和最大距离视为限制条件,可以知道,最大距离和限制条件多少具有单调性 所以可以二分最大距离,加边+check #include<cstdio> #include<algorith ...
- python登录知乎
#coding:utf-8 import requests import urllib3 import time class Zhihu: def __init__(self): self.login ...