CS20 D LCA
给出一棵树,许多询问,每次询问A,B,C三点,求一点使到三点距离最小,输出该点和最小值。
很明显就是求LCA,三种组合都求一次LCA,然后在里面选个距离和最小的就行了。
官方题解里面的代码求LCA是在线DFS RMQ的方法..先记录欧拉序,且记录某个点在序列里的第一个位置,每次询问a,b的LCA就是询问两者在欧拉序列里第一个位置之差中的那些点里面深度最小的
LCA(a,b)=RMQ(dep, pos[a], pos[b])
/** @Date : 2017-09-27 20:41:28
* @FileName: CS20 C LCA RMQ.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; int dep[N];
int pos[N];
int rmq[19][2*N];
int eul[2*N], c, l[2*N]; vector<int> edg[N]; void dfs(int x, int pre)
{
eul[++c] = x;
pos[x] = c;
if(pre) dep[x] = dep[pre] + 1;
for(auto i: edg[x])
{
if(i == pre)
continue;
dfs(i, x);
eul[++c] = x;
}
} void init()
{
dfs(1, 0);
for(int i = 2; i <= c; i++)//预处理 2^k=x对应的k
l[i] = l[i / 2] + 1;
for(int i = 1; i <= c; i++)
rmq[0][i] = eul[i];
for(int j = 1; (1 << j) <= c; j++)
for(int i = 1; i <= c; i++)
{
rmq[j][i] = rmq[j - 1][i];
if(i + (1 << (j - 1)) > c)
continue;
if(dep[rmq[j - 1][i + (1 << (j - 1))]] < dep[rmq[j][i]])
rmq[j][i] = rmq[j - 1][i + (1 << (j - 1))];
}
} int lca(int x, int y)
{
if(pos[x] > pos[y])
swap(x, y);
int dis = pos[y] - pos[x] + 1;
int k = l[dis];
if(dep[rmq[k][pos[x] + dis - (1 << k)]]
< dep[rmq[k][pos[x]]])
return rmq[k][pos[x] + dis - (1 << k)];
else return rmq[k][pos[x]];
} int distance(int a, int b)
{
int ac = lca(a, b);
return dep[a] + dep[b] - 2 * dep[ac];
}
int main()
{
int n, q;
cin >> n >> q;
for(int i = 0; i < n - 1; i++)
{
int x, y;
scanf("%d%d", &x, &y);
edg[x].PB(y);
edg[y].PB(x);
}
init();
while(q--)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
int ac1 = lca(a, b);
int ac2 = lca(a, c);
int ac3 = lca(b, c);
int ans1 = distance(ac1, a) + distance(ac1, b) + distance(ac1, c);
int ans2 = distance(ac2, a) + distance(ac2, b) + distance(ac2, c);
int ans3 = distance(ac3, a) + distance(ac3, b) + distance(ac3, c);
if(ans1 > ans2)
swap(ans1, ans2), swap(ac1, ac2);
if(ans1 > ans3)
swap(ans1, ans3), swap(ac1, ac3);
printf("%d %d\n", ac1, ans1);
}
return 0;
}
CS20 D LCA的更多相关文章
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
- BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]
3626: [LNOI2014]LCA Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2050 Solved: 817[Submit][Status ...
- [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)
Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...
- [bzoj2588][count on a tree] (主席树+lca)
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
- [板子]倍增LCA
倍增LCA板子,没有压行,可读性应该还可以.转载请随意. #include <cstdio> #include <cstring> #include <algorithm ...
- poj3417 LCA + 树形dp
Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4478 Accepted: 1292 Descripti ...
- [bzoj3626][LNOI2014]LCA
Description 给出一个$n$个节点的有根树(编号为$0$到$n-1$,根节点为$0$). 一个点的深度定义为这个节点到根的距离$+1$. 设$dep[i]$表示点$i$的深度,$lca(i, ...
- (RMQ版)LCA注意要点
inline int lca(int x,int y){ if(x>y) swap(x,y); ]][x]]<h[rmq[log[y-x+]][y-near[y-x+]+]])? rmq[ ...
- bzoj3631: [JLOI2014]松鼠的新家(LCA+差分)
题目大意:一棵树,以一定顺序走完n个点,求每个点经过多少遍 可以树链剖分,也可以直接在树上做差分序列的标记 后者打起来更舒适一点.. 具体实现: 先求x,y的lca,且dep[x]<dep[y] ...
随机推荐
- Leetcode题库——7.反转整数
@author: ZZQ @software: PyCharm @file: IntReverse.py @time: 2018/9/16 16:36 要求:整数反转(给定一个 32 位有符号整数,将 ...
- Java第一天——环境变量的配置与破解myeclipse2013
一.jdk环境变量的配置 1.下载JDK并安装(官网JavaSE,64位(具体看电脑是多少位的))官网http://www.oracle.com/technetwork/java/javase/dow ...
- 关于vue项目管理项目的架构管理平台
关于vue项目管理项目的架构管理平台 https://panjiachen.github.io/vue-element-admin-site/#/zh-cn/faq 31.4k 次浏览 完整项目地址: ...
- TiDB注意事项
公司最近在上测试的TiDB集群,这款数据库类似MySQL,但又不完全一致,在使用的时候有一下注意事项,在这里记录一下.
- java类和对象
类是对象的抽象 对象是类的一个实例类 对象 = new 类();拿对象可以操作这个类里的方法 java类与对象的区别是个老生常谈的问题,刚开始学java的时候就开始接触类和对象,今天来总结一下他们之间 ...
- js框架总结
参考地址 http://www.techweb.com.cn/network/system/2015-12-23/2245809.shtml https://www.cnblogs.com/mbail ...
- POJ2391_Ombrophobic Bovines
有F个地方,每个地方有一定数量的牛,能够容纳一定数量的牛,某些地方之间有边,表示走两点之间需要消耗的时间. 现在求使得所有的牛都被容纳所需要的最少的时间. 由于时间是一个不确定的因素,我们需要二分. ...
- 在tensorflow环境下安装matplotlib
在运行程序时,报错ImportError: No module named 'matplotlib',如图.经网上查询发现是没有安装matplotlib 因此记录一下在tensorflow环境下安装m ...
- 【转载】JSP生成静态Html页面
在网站项目中,为了访问速度加快,为了方便百度爬虫抓取网页的内容,需要把jsp的动态页面转为html静态页面.通常有2种常用的方式: 1.伪静态,使用URL Rewriter 2.纯静态,本文中代码实现 ...
- java追加写入txt文件
整理了下网上的资料,数据追加写入txt文件有三种方式,见下面代码: 方法一: public void method1() { FileWriter fw = null; try { //如果文件存在, ...