http://acm.hdu.edu.cn/showproblem.php?pid=2586

给出一颗树和边权,询问两点距离。

考虑tarjan离线做法,做法很巧妙,当前进行到u,对他的儿子v,当v子树tarjan完成之后把v合并到u上。当遍历完所有v之后,对与u有关的询问进行查找,若第二个询问点v被访问过,那么lca(u,v)就是v目前被合并到的根上。还有记录d[u]表示根到u的距离。

  最后答案就是d[u]+d[v]-2*d[lca(u,v)]。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<stack>
#include<deque>
#include<bitset>
#include<unordered_map>
#include<unordered_set>
#include<queue>
#include<cstdlib>
#include<ctype.h>
#include<ctime>
#include<functional>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define debug puts("debug")
#define mid ((L+R)>>1)
#define lc (id<<1)
#define rc (id<<1|1)
const int maxn=;
const int maxm=;
const double PI=acos(-1.0);
const double eps=1e-;
const LL mod=1e9+;
LL gcd(LL a,LL b){return b==?a:gcd(b,a%b);}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
LL qpow(LL a,LL b,LL c){LL r=; for(;b;b>>=,a=a*a%c)if(b&)r=r*a%c;return r;}
template<class T>
void prt(T v){for(auto x:v)cout<<x<<' ';cout<<endl;}
struct Edge{int u,v,w,next;}; vector<pii>g[maxn],q[maxn];
int d[maxn],f[maxn],qu[],qv[],ans[];
bool vis[maxn];
int getf(int u){return f[u]==u?u:f[u]=getf(f[u]);}
void tarjan(int u){
vis[u]=;
for(pii e:g[u]){
int v=e.fi,w=e.se;
if(!vis[v]){
d[v]=d[u]+w;
tarjan(v);
f[v]=u;
}
}
for(pii e:q[u]){
int v=e.fi,id=e.se;
if(vis[v]){
ans[id]=getf(v);
}
}
}
int main(){
int t,n,m,i,j,u,v,w;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(i=;i<=n;++i){
g[i].clear();
q[i].clear();
f[i]=i;
vis[i]=;
}
for(i=;i<n;++i){
scanf("%d%d%d",&u,&v,&w);
g[u].pb(mp(v,w));
g[v].pb(mp(u,w));
}
for(i=;i<=m;++i){
scanf("%d%d",qu+i,qv+i);
q[qu[i]].pb(mp(qv[i],i));
q[qv[i]].pb(mp(qu[i],i));
}
tarjan();
for(i=;i<=m;++i){
printf("%d\n",d[qu[i]]+d[qv[i]]-*d[ans[i]]);
}
}
return ;
}

接着用ST在线做法又做了一遍。如果题目强制在线的话,tarjan做法就gg了,我们提前不知道询问便无法离线做了。

ST做法是每次访问到一个节点就记录下当前的节点值和深度,当查询lca(u,v)的时候,先找到u和v第一次访问到的位置L和R(L<=R),

然后在[L,R]中找到一个深度最小的点,他对应的节点值w=lca(u,v)。RMQ问题,使用ST处理O(nlog(n))。询问就可以O(1)啦。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<stack>
#include<deque>
#include<bitset>
#include<unordered_map>
#include<unordered_set>
#include<queue>
#include<cstdlib>
#include<ctype.h>
#include<ctime>
#include<functional>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define debug puts("debug")
#define mid ((L+R)>>1)
#define lc (id<<1)
#define rc (id<<1|1)
const int maxn=;
const int maxm=;
const double PI=acos(-1.0);
const double eps=1e-;
const LL mod=1e9+;
LL gcd(LL a,LL b){return b==?a:gcd(b,a%b);}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
LL qpow(LL a,LL b,LL c){LL r=; for(;b;b>>=,a=a*a%c)if(b&)r=r*a%c;return r;}
template<class T>
void prt(T v){for(auto x:v)cout<<x<<' ';cout<<endl;}
struct Edge{int u,v,w,next;}; vector<pii>g[maxn];
vector<int>dep,idx;
int f[maxn*][],d[maxn],p[maxn];
void dfs(int u,int o,int fa){
p[u]=idx.size();
idx.pb(u),dep.pb(o);
for(pii e:g[u]){
int v=e.fi,w=e.se;
if(v!=fa){
d[v]=d[u]+w;
dfs(v,o+,u);
idx.pb(u),dep.pb(o);
}
}
}
void ST(int n){
for(int i=;i<n;++i)f[i][]=i;
for(int j=;(<<j)<=n;j++){
for(int i=;(i+(<<j)-)<n;i++){
f[i][j]=dep[f[i][j-]]<dep[f[i+(<<(j-))][j-]]?f[i][j-]:f[i+(<<(j-))][j-];
}
}
}
int ask(int L,int R){
int k=;
while((<<(+k))<=R-L+)k++;
return dep[f[L][k]]<dep[f[R-(<<k)+][k]]?idx[f[L][k]]:idx[f[R-(<<k)+][k]];
}
int main(){
int t,n,m,i,j,u,v,w;
scanf("%d",&t);
while(t--){
idx.clear();
dep.clear();
scanf("%d%d",&n,&m);
for(i=;i<=n;++i){
g[i].clear();
}
for(i=;i<n;++i){
scanf("%d%d%d",&u,&v,&w);
g[u].pb(mp(v,w));
g[v].pb(mp(u,w));
}
dfs(,,);
ST(idx.size());
// prt(idx);
// prt(dep);
while(m--){
scanf("%d%d",&u,&v);
int L=p[u],R=p[v];
if(L>R)swap(L,R);
printf("%d\n",d[u]+d[v]-*d[ask(L,R)]);
}
}
return ;
}
/*
5
7 7
1 2 1
1 3 1
2 4 1
2 5 1
5 6 1
5 7 1
*/

  综上两种方法而言,我感觉思想都是类似是,就是u-->v这条路一定经过他们的共同祖先,这中间过程中经过若干个点,我们要想办法找到深度最小的那个点,显然他只会被经过一次,就是LCA。

HDU-2586-裸LCA入门-tarjan离线的更多相关文章

  1. hdu 2586(裸LCA)

    传送门 题意: 某村庄有n个小屋,n-1条道路连接着n个小屋(无环),求村庄A到村庄B的距离,要求是经过任一村庄不超过一次. 题解: 求出 lca = LCA(u,v) , 然后答案便是dist[u] ...

  2. hdu2586(lca模板 / tarjan离线 + RMQ在线)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意: 给出一棵 n 个节点的带边权的树, 有 m 个形如 x y 的询问, 要求输出所有 x, ...

  3. 洛谷 P3379 【模板】最近公共祖先(LCA)Tarjan离线

    题目链接:LCA tarjan离线 这道题目WA无数发,最后还是参考了大神的blog 谁会想到因为一个输入外挂WA呢 大概是我的挂是假挂吧...orz(其实加上外挂,速度提升很多) 用链式前向星保存边 ...

  4. POJ 1330 Nearest Common Ancestors 【最近公共祖先LCA算法+Tarjan离线算法】

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20715   Accept ...

  5. HDU 2586.How far away ?-离线LCA(Tarjan)

    2586.How far away ? 这个题以前写过在线LCA(ST)的,HDU2586.How far away ?-在线LCA(ST) 现在贴一个离线Tarjan版的 代码: //A-HDU25 ...

  6. HDU 2586 How far away ? 离线lca模板题

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. LCA之tarjan离线

    显然81篇题解是有点多了,不让我提交. 更为不好的是没有一篇详细的\(tarjan\)(不过我也不会写详细的). 不过\(tarjan\)并没有我们想象的那样难理解,时间也并不爆炸(巧妙的跳过难写二字 ...

  8. How far away ? HDU - 2586 【LCA】【RMQ】【java】

    题目大意:求树上任意两点距离. 思路: dis[i]表示i到根的距离(手动选根),则u.v的距离=dis[u]+dis[v]-2*dis[lca(u,v)]. lca:u~v的dfs序列区间里,深度最 ...

  9. POJ 1986 Distance Queries 【输入YY && LCA(Tarjan离线)】

    任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total ...

随机推荐

  1. vs编译出现 fatal error LNK1281:无法生成 SAFESEH 映像

    问题: 在vs编译中我们有时候常常会见到这样的错误,无法生成 SAFESEH 映像,镜像安全问题 解决方法: 1.打开该项目的"属性页"对话框. 2.单击"链接器&quo ...

  2. mutect/mutsig/gistic官网汇总

    http://software.broadinstitute.org/software/cprg/

  3. hdu 6053 TrickGCD 筛法

    TrickGCD Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Probl ...

  4. 阿里云CentOS Linux服务器上搭建邮件服务器遇到的问题

    参考文章: 阿里云CentOS Linux服务器上用postfix搭建邮件服务器 Linux系统下邮件服务器的搭建(Postfix+Dovecot) 本来想自己搭建邮件服务器,但是看到一篇资料表示阿里 ...

  5. String和常量池

    1.Java 会确保一个字符串常量只有一个拷贝 2.用new String() 创建的字符串不是常量,不能在编译期就确定,所以new String() 创建的字符串不放入常量池中,它们有自己的地址空间 ...

  6. [原][osg]osg文件与osgb文件的区别

    osgb是二进制文件 osg是文本文件 osgb可以通过osgconv.exe转换成osg文件 osgb可以包含纹理文件(就是贴图) osg只能连接外部纹理文件(是的,你看到的cow.osg都是用的外 ...

  7. Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0 解决方法: 要对切割字符进行转义\\

    使用str.split("[",15)时,出现Exception in thread "main" java.util.regex.PatternSyntaxE ...

  8. java与js交互,相互调用传参

    随着前端技术的发展与H5的广泛使用,移动端采用native+h5的方式越来越多了,对于Android来说就涉及到java与js的交互,相互调用传参等.下面就来看一下java与js交互的简单demo. ...

  9. highchart柱状堆叠图动态数据请求

    $(function () { var options = { chart: { renderTo: 'indoor', type: 'column', }, title: { text: '室内问题 ...

  10. windows 网卡配置的设置命令

    (1)设置为DHCP自动分配 netsh interface ip set address "本地连接"  dhcp netsh interface ip set dns &quo ...