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] ...
随机推荐
- HTML和CSS <h1> --1-- <h1>
Html和CSS的关系 学习web前端开发基础技术需要掌握:HTML.CSS.JavaScript语言.下面我们就来了解下这三门技术都是用来实现什么的: 1. HTML是网页内容的载体.内容就是网页制 ...
- DPDK skeleton basicfwd 源码阅读
学习这个例子用于理解单纯的 dpdk 转发过程,L2 和 L3 的转发是基于此:在rte_eth_rx_burst()收包后进行解包,提取 mac.ip 等信息然后在转发到输出网卡. 如果要写出自己的 ...
- vim python和golang开发环境配置
首先在-下新建目录.vim和配置文件.vimrc,.vimrc内容如下: syntax on set nocompatible filetype off set rtp+=~/.vim/bundle/ ...
- 由RS-232串口到PROFIBUS-DP总线的转换接口设计
转自:http://gongkong.ofweek.com/2013-08/ART-310007-11001-28716256_2.html 1.PROFIBUS-DP网络协议 PROFIBUS的网络 ...
- 模拟alert,confirm 阻塞状态
/*** * 模拟alert弹窗 * content 为弹框显示的内容 * 确定按钮对应的下面取消关闭显示框 * **/function oAlert(content) { var oWrap = $ ...
- matlab的应用
MATLAB在信号与系统中的应用 一: 看到MATLAB在信号与系统中的应用,对这部分我比较熟悉,在此举一个小例子. impulse(sys)表示求连续系统sys的冲击响应 subplot ...
- ISCC2018(misc)
ISCC2018 misc writeup(部分) 这些天做个了iscc题目,有些题目不是很难,网上都有相同的题或者类似的题目,但是我很菜,没做出来多少. #misc1:Where is the FL ...
- 对一致性Hash算法及java实现(转)
一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细解读一文中"一致性Hash算法"部分,对于为什么要使用一致性Hash算法.一致性 ...
- docker 建立私有仓库,24.205为镜像仓库所在主机
一.下载registry #yum install -y python-devel libevent-devel python-pip gcc xz-devel #python-pip install ...
- iptables 开放端口
#iptables -A INPUT -p tcp --dport 5000 -j ACCEPT #service iptables save