距离LCA离线算法Tarjan + dfs + 并查集
还是普通的LCA但是要求的是两个节点之间的距离,学到了一些
一开始我想用带权并查集进行优化,但是LCA合并的过程晚于离线计算的过程,所以路径长度会有所偏差
所以失败告终
网上查询之后懂得要提前进行一下预处理,在输入完全部的边之后,也就是数形成之后,计算dis——》也就是每个点到树根的长度
之后进行询问查询时:u,v 和 rt 这样uv的距离就是dis[u] + dis[v] - 2 * dis[rt]很好理解
时间复杂度也还可以
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
const int maxn = 5e4 + 50;
const int maxm = 7e4 + 6e3;
int id[maxn],qid[maxn];
int cnt,qcnt;
int pre[maxn],cost[maxn];
int vis[maxn];
struct node{
int to,pre,cost;
}e[maxn * 2];
struct node2{
int to,ads,pre;
}q[maxm * 2];
int ans[maxm];
int Find(int x)
{
//cout<<x<<endl;
if(pre[x] == x)return x;
else
{
//cost[x] += cost[pre[x]];
return pre[x] = Find(pre[x]);
}
} void join(int a,int b)
{
int fa = Find(a),fb = Find(b);
if(fa != fb)
{
pre[fb] = fa;
}
} void init(int n)
{
for(int i = 0;i <= n;i++)
{
pre[i] = i;
vis[i] = 0;
cost[i] = 0;
}
memset(id,-1,sizeof(id));
memset(qid,-1,sizeof(qid));
cnt = qcnt = 0;
} void add(int from,int to,int cost)
{
e[cnt].to = to;
e[cnt].pre = id[from];
e[cnt].cost = cost;
id[from] = cnt++;
}
void qadd(int from,int to,int i)
{
q[qcnt].to = to;
q[qcnt].ads = i;
q[qcnt].pre = qid[from];
qid[from] = qcnt++;
}
void get_cost(int rt,int dis)
{
vis[rt] = 1;
cost[rt] = dis;
for(int i = id[rt];~i;i = e[i].pre)
{
int to = e[i].to;
int cos = e[i].cost;
if(!vis[to])
{
get_cost(to,dis+cos);
}
}
}
void tarjan(int rt)
{
//cout<<rt<<endl;
vis[rt] = -1;
for(int i = id[rt];~i;i = e[i].pre)
{
int to = e[i].to;
int cos = e[i].cost;
if(!vis[to])
{
tarjan(to);
join(rt,to);
}
//cout<<"cs"<<to<<" "<<cost[to]<<endl;
} vis[rt] = 1;
for(int i = qid[rt];~i;i = q[i].pre)
{
int to = q[i].to;
if(vis[to] == 1)
{
//cout<<"to : "<<to<<endl;
//cout<<"pre[to]: "<<pre[to]<<endl;
//cout<<rt<<" "<<to<<"cost: "<<cost[rt]<<" "<<cost[to]<<endl;
int lca = Find(to);
ans[q[i].ads] = abs(cost[lca] - cost[rt]) + abs(cost[lca] - cost[to]);
}
}
}
int main()
{
int n,m,u,v,w;
scanf("%d",&n);
init(n);
for(int i = 1;i < n;++i)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
get_cost(0,0);
memset(vis,0,sizeof(vis));
scanf("%d",&m);
for(int i = 1;i <= m;++i)
{
scanf("%d%d",&u,&v);
qadd(u,v,i);
qadd(v,u,i);
}
tarjan(0);
for(int i = 1;i <= m;++i)
{
printf("%d\n",ans[i]);
}
return 0;
}
/*
7
0 1 1
0 2 1
0 3 1
1 4 1
2 5 1
2 6 1
40
0 1
0 2
0 3
0 4
0 5
0 6
0 0
1 1
1 2
1 3
1 4
1 5
1 6
1 0
2 0
2 1
2 2
2 3
2 4
2 5
2 6
3 0
3 1
3 2
3 3
3 4
3 5
3 6
4 0
4 1
4 2
4 3
4 4
4 5
4 6
5 0
5 1
5 2
5 3
5 4
*/
距离LCA离线算法Tarjan + dfs + 并查集的更多相关文章
- LCA离线算法Tarjan详解
离线算法也就是需要先把所有查询给保存下来,最后一次输出结果. 离线算法是基于并查集实现的,首先就是初始化P[i] = i. 接下来对于每个点进行dfs: ①首先判断是否有与该点有关的查询,如果当前该点 ...
- LCA离线算法Tarjan的模板
hdu 2586:题意:输入n个点的n-1条边的树,m组询问任意点 a b之间的最短距离 思路:LCA中的Tarjan算法,RMQ还不会.. #include <stdio.h> #inc ...
- HDU 2874 LCA离线算法 tarjan算法
给出N个点,M条边.Q次询问 Q次询问每两点之间的最短距离 典型LCA 问题 Marjan算法解 #include "stdio.h" #include "strin ...
- POJ1986 DistanceQueries 最近公共祖先LCA 离线算法Tarjan
这道题与之前那两道模板题不同的是,路径有了权值,而且边是双向的,root已经给出来了,就是1,(这个地方如果还按之前那样来计算入度是会出错的.数据里会出现多个root...数据地址可以在poj的dis ...
- Tarjan的LCA离线算法
LCA(Least Common Ancestors)是指树结构中两个结点的最低的公共祖先.而LCA算法则是用于求两个结点的LCA.当只需要求一对结点的LCA时,我们很容易可以利用递归算法在O(n)的 ...
- Leetcode题目200.岛屿数量(BFS+DFS+并查集-中等)
题目描述: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...
- LCA(最近公共祖先)离线算法Tarjan+并查集
本文来自:http://www.cnblogs.com/Findxiaoxun/p/3428516.html 写得很好,一看就懂了. 在这里就复制了一份. LCA问题: 给出一棵有根树T,对于任意两个 ...
- LCA 离线的Tarjan算法 poj1330 hdu2586
LCA问题有好几种做法,用到(tarjan)图拉算法的就有3种.具体可以看邝斌的博客.http://www.cnblogs.com/kuangbin/category/415390.html 几天的学 ...
- POJ 1470 Closest Common Ancestors (最近公共祖先LCA 的离线算法Tarjan)
Tarjan算法的详细介绍,请戳: http://www.cnblogs.com/chenxiwenruo/p/3529533.html #include <iostream> #incl ...
随机推荐
- Spring Boot 2.0(二):Spring Boot 开源软件都有哪些?(转)
2016年 Spring Boot 还没有被广泛使用,在网上查找相关开源软件的时候没有发现几个,到了现在经过2年的发展,很多互联网公司已经将 Spring Boot 搬上了生产,而使用 Spring ...
- 安全概念:DMZ(非军事化区,隔离区)
DMZ是英文“demilitarized zone”的缩写,中文名称为“隔离区”,也称“非军事化区”.它是为了解决安装防火墙后外部网络不能访问内部网络服务器的问题,而设立的一个非安全系统与安全系统之间 ...
- 品味性能之道<十一>:JAVA中switch和if性能比较
通常而言大家普遍的认知里switch case的效率高于if else.根据我的理解而言switch的查找类似于二叉树,if则是线性查找.按照此逻辑推理对于对比条件数目大于3时switch更优,并且对 ...
- Servlet API
Servlet API的查询网址:通过Tomcat的官网链接找到 可见,Servlet API有4个packages javax.servlet // 包含定义Servlet和Servlet容器之间契 ...
- 使用iTEXT库生成pdf
iTEXT下载地址 https://sourceforge.net/projects/itext/files/ 选择绿色的按钮,下载最新版本,解压后是一些jar包 为了使用方便,将文件夹放到JAVA_ ...
- PHP 过滤特殊符号
function strFilter($str){ $str = str_replace('`', '', $str); $str = str_replace('·', '', $str); $str ...
- Win 10 关闭了.net framework 3.5后再开启不成功
背景: Win10 想要使用IETester来测试下页面的浏览器兼容性.在“控制面板”里关闭了.net framework 3.5,再重新开启. 问题: 在重新开启的时候,提示不成功.错误代码0x80 ...
- Bootstrap学习遇到的role属性--- 无障碍网页应用属性
以前接触过Bootstrap,但也只是仅仅接触,现在重新学习下,今天看到一个例子中的属性有一个role, 查阅资料发现这个是--WAI-ARIA无障碍设计属性: 通俗说是该设计为了一些盲人,失聪,残疾 ...
- 各种 on事件触发js代码
[转]各种 on事件触发js代码 1.onmouseenter:当鼠标进入选区执行代码 <div style="background-color:red" onmouseen ...
- 【linux轻松学】修改文件权限
用chmod修改文件权限,此命令非常重要. 用户范围:u 表示当前用户g 表示当前群组o 除u,g之外的用户和群组a 所有用户和群组 权限代号:r :读,用数字4表示w :写,用数字2表示x :执行, ...