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

给定一棵带权有根树,对于m个查询(u,v),求得u到v之间的最短距离

那么仅仅要求得LCA(u,v),dis(u,v)=dis[u]+dis[v]-2*dis[LCA(u,v)]。当中dis[i]表示节点i到根节点root的距离

31MS 4104K 2186 B

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
const int maxn=40010;
const int maxq=210;
using namespace std;
int n,m;
struct Edge{
int to,w;
int next;
}edge[maxn<<1];int head[maxn],tot;
struct Query{
int to,next;
int index;
}que[maxq<<1];int h[maxn],tt; void addedge(int u,int v,int w){
edge[tot].to=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
void add_query(int u,int v,int index){
que[tt].to=v;
que[tt].index=index;
que[tt].next=h[u];
h[u]=tt++;
}
int f[maxn],dis[maxn],answer[maxq];
bool vis[maxn],in[maxn];
int find(int x){
return f[x]==-1 ? x:f[x]=find(f[x]);
}
void Union(int a,int b){
int t1=find(a);
int t2=find(b);
if(t1!=t2) f[t2]=t1;
}
void LCA(int u){
vis[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(vis[v]) continue;
dis[v]=dis[u]+edge[i].w;
LCA(v);
Union(u,v);
}
for(int i=h[u];i!=-1;i=que[i].next){
int v=que[i].to;
if(vis[v]){
answer[que[i].index]=dis[u]+dis[v]-2*dis[find(v)];
}
}
}
void Init(){
tot=tt=0;
memset(h,-1,sizeof(h));
memset(head,-1,sizeof(head));
memset(f,-1,sizeof(f));
memset(vis,false,sizeof(vis));
memset(in,false,sizeof(in));
memset(dis,0,sizeof(dis));
}
int main()
{
//#ifndef ONLINE_JUDGE
//freopen("in.cpp","r",stdin);
//#endif // ONLINE_JUDGE
int T,u,v,w;
cin>>T;
while(T--){
Init();
scanf("%d%d",&n,&m);
for(int i=0;i<n-1;++i){
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
in[v]=true;
}
for(int i=0;i<m;++i){
scanf("%d%d",&u,&v);
add_query(u,v,i);
add_query(v,u,i);
}
for(int i=1;i<=n;++i){
if(in[i]==false){
LCA(i);
break;
}
}
for(int i=0;i<m;++i){
printf("%d\n",answer[i]);
}
}
return 0;
}

HDU2586.How far away ?——近期公共祖先(离线Tarjan)的更多相关文章

  1. POJ1330Nearest Common Ancestors——近期公共祖先(离线Tarjan)

    http://poj.org/problem? id=1330 给一个有根树,一个查询节点(u,v)的近期公共祖先 836K 16MS #include<iostream> #includ ...

  2. 图论-最近公共祖先-离线Tarjan算法

    有关概念: 最近公共祖先(LCA,Lowest Common Ancestors):对于有根树T的两个结点u.v,最近公共祖先表示u和v的深度最大的共同祖先. Tarjan是求LCA的离线算法(先存储 ...

  3. POJ 1330 LCA最近公共祖先 离线tarjan算法

    题意要求一棵树上,两个点的最近公共祖先 即LCA 现学了一下LCA-Tarjan算法,还挺好理解的,这是个离线的算法,先把询问存贮起来,在一遍dfs过程中,找到了对应的询问点,即可输出 原理用了并查集 ...

  4. [HDOJ2586]How far away?(最近公共祖先, 离线tarjan, 并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 这题以前做过…现在用tarjan搞一发…竟然比以前暴力过的慢………… 由于是离线算法,需要Que ...

  5. 近期公共祖先(LCA)——离线Tarjan算法+并查集优化

    一. 离线Tarjan算法 LCA问题(lowest common ancestors):在一个有根树T中.两个节点和 e&sig=3136f1d5fcf75709d9ac882bd8cfe0 ...

  6. HDU 2586 How far away ?(LCA模板 近期公共祖先啊)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the vi ...

  7. LCA 近期公共祖先 小结

    LCA 近期公共祖先 小结 以poj 1330为例.对LCA的3种经常使用的算法进行介绍,分别为 1. 离线tarjan 2. 基于倍增法的LCA 3. 基于RMQ的LCA 1. 离线tarjan / ...

  8. LCA近期公共祖先

    LCA近期公共祖先 该分析转之:http://kmplayer.iteye.com/blog/604518 1,并查集+dfs 对整个树进行深度优先遍历.并在遍历的过程中不断地把一些眼下可能查询到的而 ...

  9. POJ 1470 Closest Common Ancestors【近期公共祖先LCA】

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013912596/article/details/35311489 题目链接:http://poj ...

  10. LintCode 近期公共祖先

    中等 近期公共祖先 查看执行结果 34% 通过 给定一棵二叉树,找到两个节点的近期公共父节点(LCA). 近期公共祖先是两个节点的公共的祖先节点且具有最大深度. 您在真实的面试中是否遇到过这个题? Y ...

随机推荐

  1. linux静态链接库与动态链接库详解

    一顺便说说了哦  通常情况下,对函数库的链接是放在编译时期(compile time)完成的.所有相关的对象文件(object file)与牵涉到的函数库(library)被链接合成一个可执行文件(e ...

  2. nodejs升级

    命令如下: sudo npm install n -g 然后就可以使用n命令: sudo n 0.12.2 这个命令是将nodejs升级到0.12.2版本. sudo n stable 这个命令是升级 ...

  3. window.onunload | window.onbeforeunload

    先引述一段jQuery 官方对于onunload的评述: The unload event is sent to the window element when the user navigates ...

  4. Windows快捷键命令

    1. 新建一个文件夹: Ctrl + shift + N; 2. Windows 查看端口信息: 1.进入cmd窗口; 2.netstat -ano : 列出所有端口的情况.在列表中我们观察被占用的端 ...

  5. 【python下使用OpenCV实现计算机视觉读书笔记1】输入输出

    说明: 该部分内容为<OpenCV Computer Vision with Python>读书笔记. 1.读入文件与保存. import cv2 image=cv2.imread('a. ...

  6. LIntcode---将二叉搜索树转成较大的树

    Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...

  7. Mysql中count(*),DISTINCT的使用方法和效率研究

    在处理一个大数据量数据库的时候 突然发现mysql对于count(*)的不同处理会造成不同的结果 比如执行 SELECT count(*) FROM tablename 即使对于千万级别的数据mysq ...

  8. 第7章 Iptables与Firewalld防火墙。

    第7章 Iptables与Firewalld防火墙.     Chapter7_听较强节奏的音乐能够让您更长时间的投入在学习中. <Linux就该这么学> 00:00/00:00     ...

  9. web.py+fastcgi+nginx 502错误解决

    用web.py照着官网在服务器上搭好了后台.这次很奇怪地出现了一个Nginx 502 Bad Gateway的错误. 执行上面的kill `pgrep -f "python /path/to ...

  10. GetLastError 错误码大全(转载)

    转载自:GetLastError GetLastError GetLastError返回的值通过在api函数中调用SetLastError或SetLastErrorEx设置.函数   并无必要设置上一 ...