距离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 ...
随机推荐
- N! java
import java.util.*; import java.math.*; public class Num2{ public static void main(String args[]){ B ...
- XiaoKL学Python(D)argparse
该文以Python 2为基础. 1. argparse简介 argparse使得编写用户友好的命令行接口更简单. argparse知道如何解析sys.argv. argparse 模块自动生成 “帮助 ...
- linux内socket服务器无法连接windows
今天在试socket的时候出现了一个问题:问题概述是这样的: 1.linux采用centOS7(mini)版本,虚拟机版本VMware12,网卡设置NAT 2.服务器和客户端都在windows上,通讯 ...
- 开发apicloud模块遇到的几个梗
2017-06-04 原来模块中不能的R.id.xxx,只能用UZResourcesIDFinder.getResIdID("mo_minivr_framecontainer") ...
- 调用webservice时,产生android.os.NetworkOnMainThreadException错误
android.os.NetworkOnMainThreadException 网上搜索后知道是因为版本问题,在4.0之后在主线程里面执行Http请求都会报这个错,也许是怕Http请求时间太长造成程序 ...
- django 静态文件
django中的静态文件,如图片,css样式jquery等等 在url最下面加上 from django.conf.urls.static import staticfrom django.conf ...
- iOS中四种实例变量的范围类型@private@protected@public@package
文档上记录是这样的 The Scope of Instance Variables Toenforce the ability of an object to hide its data, the c ...
- 基于Web Service的客户端框架搭建一:C#使用Http Post方式传递Json数据字符串调用Web Service
引言 前段时间一直在做一个ERP系统,随着系统功能的完善,客户端(CS模式)变得越来越臃肿.现在想将业务逻辑层以下部分和界面层分离,使用Web Service来做.由于C#中通过直接添加引用的方来调用 ...
- ubunut下安装ibus_pinyin中文输入法
ubuntu安装中文输入法,,此处一ibus-pinyin为例为其安装中文输入法,,, 1. 设置(setting)---语言支持(language support)---汉语(chinese),,, ...
- 219.01.19 bzoj3252: 攻略(长链剖分+贪心)
传送门 长链剖分好题. 题意:给一棵带点权的树,可以从根节点到任一叶节点走kkk次,走过的点只能计算一次,问kkk次走过的点点权值和最大值. 思路: 考虑将整棵树带权长链剖分,这样链与链之间是不会重复 ...