Tarjan_LCA
貌似求LCA使用倍增已经可以应付掉大多数需要LCA的题了..
但是有些时候$O(MlogN)$的复杂度就不可接受了
Tarjan_LCA对于每个询问采用离线处理
总复杂度为$O(M+N)$
这个复杂度几乎不可能被卡掉
简单说的话用Tarjan求LCA就是根据后序dfs的框架然后用并查集加持。
具体实现过程参加代码。
用HDU2586作为模板
//HUD 2586 Tarjan_LCA
//by Cydiater
//2016.8.15
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <queue>
#include <map>
#include <iomanip>
#include <algorithm>
using namespace std;
#define ll long long
#define up(i,j,n) for(int i=j;i<=n;i++)
#define down(i,j,n) for(int i=j;i>=n;i--)
const int MAXN=1e6+5;
const int oo=0x3f3f3f3f;
inline int read(){
char ch=getchar();int x=0,f=1;
while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int N,M,LINK[MAXN],len=0,dis[MAXN],fa[MAXN],tol=0,Link[MAXN],g[MAXN],T,ans[MAXN];
bool vis[MAXN];
struct edge{int y,next,v;}e[MAXN];
struct query{int y,next,lca;}q[MAXN];
namespace solution{
inline void insert(int x,int y,int v){e[++len].next=LINK[x];LINK[x]=len;e[len].y=y;e[len].v=v;}
inline void Insert(int x,int y){q[++tol].next=Link[x];Link[x]=tol;q[tol].y=y;}
int get(int k){
if(g[k]==k) return k;
g[k]=get(g[k]);
return g[k];
}
void dfs(int node,int dist,int father){
fa[node]=father;dis[node]=dist;
for(int i=LINK[node];i;i=e[i].next)
if(e[i].y!=father)
dfs(e[i].y,dist+e[i].v,node);
}
void init(){
N=read();M=read();
up(i,1,N-1){
int x=read(),y=read(),v=read();
insert(x,y,v);
insert(y,x,v);
}
memset(fa,0,sizeof(fa));
memset(dis,0,sizeof(dis));
memset(vis,0,sizeof(vis));
dfs(1,0,0);
up(i,1,M){
int x=read(),y=read();
Insert(x,y);
}
}
void lca(int node){
vis[node]=1;g[node]=node;
for(int i=LINK[node];i;i=e[i].next)
if(!vis[e[i].y]){
lca(e[i].y);
g[e[i].y]=node;
}
for(int i=Link[node];i;i=q[i].next)
if(vis[q[i].y]){
q[i].lca=get(q[i].y);
ans[i]=dis[node]+dis[q[i].y]-2*dis[q[i].lca];
}
}
void output(){
up(i,1,M)printf("%d\n",ans[i]);
}
}
int main(){
//freopen("input.in","r",stdin);
using namespace solution;
T=read();
while(T--){
tol=0;len=0;
memset(Link,0,sizeof(Link));
memset(LINK,0,sizeof(LINK));
init();
lca(1);
output();
}
return 0;
}
Tarjan_LCA的更多相关文章
- hud 2586 How far away ?
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Description There are n houses in ...
- LCA专题
标签(空格分隔): LCA 我的个人网站挂了,最近就先用这个来写博客吧.以后争取在这个网站写一些与OI无关的个人爱好的东西. 题目来源:code[VS] 倍增--在线算法 用 $f[i][j]$ 记录 ...
- LCA 各种神奇的LCA优化方法
LCA(Least Common Ancestors) 树上问题的一种. 朴素lca很简单啦,我就不多说了,时间复杂度n^2 1.倍增LCA 时间复杂度 nlongn+klogn 其实是一种基于朴素l ...
- 最近公共祖先(LCA)的三种求解方法
转载来自:https://blog.andrewei.info/2015/10/08/e6-9c-80-e8-bf-91-e5-85-ac-e5-85-b1-e7-a5-96-e5-85-88lca- ...
- LCA的倍增算法
LCA,即树上两点之间的公共祖先,求这样一个公共祖先有很多种方法: 暴力向上:O(n) 每次将深度大的点往上移动,直至二者相遇 树剖:O(logn) 在O(2n)预处理重链之后,每次就将深度大的沿重链 ...
- NOIP2016天天爱跑步 题解报告【lca+树上统计(桶)】
题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.«天天爱跑步»是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏的地图可以看作一一棵包含 nn个 ...
- AC日记——货车运输 codevs
3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Descri ...
- LCA最近公共祖先知识点整理
题解报告:hdu 2586 How far away ? Problem Description There are n houses in the village and some bidirect ...
- BZOJ3331 [BeiJing2013]压力[圆方树+树上差分]
圆方树新技能get.具体笔记见图连通性问题学习笔记. 这题求无向图的必经点,这个是一个固定套路:首先,一张连通的无向图中,每对点双和点双之间是以一个且仅一个割点连接起来的(如果超过一个就不能是割点了) ...
随机推荐
- Bootstrap系列 -- 4. 文本内容强调
一. 文本强调基本样式 .text-muted:提示,使用浅灰色(#999) .text-primary:主要,使用蓝色(#428bca) .text-success:成功,使用浅绿色(#3c763d ...
- GWT-Dev-Plugin(即google web toolkit developer plugin)for firefox的下载地址
如果FireFox的版本为20,则对应google-web-toolkit的插件离线下载地址,不要用浏览器直接下载,用Flashget等客户端下载,超快. http://google-web-tool ...
- Resharper快捷键
建议你使用 Reshaper 的快捷键,不要担心 Reshaper 会把你原来的快捷键设置给覆盖了,因为如果某个快捷键和 VS 是冲突的,Reshaper会让你自己选择需要使用 VS 还是 Resha ...
- 来个linq to js
说这个话题之前,我们来讲一下C#的linq 语法.在C#里面我们会对列表进行操作,如OrderBy(p=>p.property),Where(p=>p.property==..) 括号里 ...
- Webmin|Linux管理员远程管理工具
介绍: Webmin is a web-based interface for system administration for Unix. Using any modern web browser ...
- 【转】一个DIV+CSS代码布局的简单导航条
原文地址:http://www.divcss5.com/shili/s731.shtml 简单的DIV CSS代码布局实现导航条 一个蓝色主题的导航条布局案例,本CSS小实例,采用DIV CSS实现. ...
- Linux_rsylogd日志轮替(三)
一.轮替规则及配置文件:vi /etc/logrotate.conf 1.如果配置文件中拥有" dateext"参数,那么日志会用日期来作为日志文件的后缀,例如" sec ...
- 【BZOJ 1038】【ZJOI 2008】瞭望塔
http://www.lydsy.com/JudgeOnline/problem.php?id=1038 半平面交裸题,求完半平面后在折线段上的每个点竖直向上和半平面上的每个点竖直向下求距离,统计最小 ...
- AOPR弹出Order Now窗口怎么办
当我们忘记了我们自己设置的office密码的时候,需要一款office密码破解软件来帮我们破解,Advanced Office Password Recovery就是这样的一款软件,其简称AOPR.试 ...
- apache ab压力测试
今天提到压力测试,想起以前看到的ab,于是又重新查找了下资料,并记录了下. ab命令会创建很多的并发访问线程,模拟多个访问者同时对某一URL地址进行访问. 它的测试目标是基于URL的,因此,既可以用来 ...