题目链接

Problem Description
There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people.
 
Input
First line is a single integer T(T<=10), indicating the number of test cases.
  For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated by a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
  Next m lines each has distinct integers i and j, you are to answer the distance between house i and house j.
 
Output
For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
 
Sample Input
2
3 2
1 2 10
3 1 15
1 2
2 3
 
 
2 2
1 2 100
1 2
2 1
 
Sample Output
10
25
100
100

题意:有一棵有n个节点的树,每条边上有一个权值代表这两个点之间的距离,现在m次询问:从节点a到节点b的路径长?

思路:预处理所有节点到根节点(定为节点1)的距离,以及所有节点的祖先信息(fa[i][j]表示节点 i 向上距离为 (1<<j)的祖先节点编号),计算a和b到根节点的距离和,减去两倍的最近公共祖先的到根节点的距离值。

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 4e4 + ; int head[N], cnt;
struct Edge
{
int to, next;
int value;
}e[ * N]; struct Node {
int fa[];
int deep;
int sum;
bool state;
}node[N]; void insert(int u, int v, int value)
{
e[++cnt].to = v;
e[cnt].next = head[u];
e[cnt].value = value;
head[u] = cnt;
e[++cnt].to = u;
e[cnt].next = head[v];
e[cnt].value = value;
head[v] = cnt;
}
int cal(int x, int t)
{
for (int i = ; i <= ; i++)
if (t&( << i)) x = node[x].fa[i];
return x;
}
void dfs(int x)
{
node[x].state = ;
for (int i = ; i <= ; i++)
{
if (node[x].deep<( << i))break;
node[x].fa[i] = node[node[x].fa[i - ]].fa[i-];///倍增处理祖先信息
}
for (int i = head[x]; i; i = e[i].next)
{
if (node[e[i].to].state) continue;
node[e[i].to].deep = node[x].deep+ ;
node[e[i].to].fa[] = x;
node[e[i].to].sum = node[x].sum+e[i].value;
dfs(e[i].to);
}
}
int lca(int x, int y)///求lca
{
if (node[x].deep<node[y].deep) swap(x, y);
x = cal(x, node[x].deep - node[y].deep);
for (int i = ; i >= ; i--)
if (node[x].fa[i] != node[y].fa[i])
{
x = node[x].fa[i];
y = node[y].fa[i];
}
if (x == y)return x;
else return node[x].fa[];
} void init()
{
cnt = ;
memset(head, , sizeof(head));
memset(node, , sizeof(node));
} int main()
{
int T; cin >> T;
while (T--)
{
init();
int n, m; cin >> n >> m;
for (int i = ; i < n-; i++) {
int x, y, v; scanf("%d%d%d",&x,&y,&v);
insert(x,y,v);
}
dfs();
for (int i = ; i < m; i++) {
int x, y; scanf("%d%d",&x,&y);
int pa = lca(x, y);
int ans = node[x].sum - node[pa].sum + node[y].sum - node[pa].sum;
cout << ans << endl;
}
}
return ;
}

LCA最近公共祖先-- HDU 2586的更多相关文章

  1. lca 最近公共祖先

    http://poj.org/problem?id=1330 #include<cstdio> #include<cstring> #include<algorithm& ...

  2. Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载)

    Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载) 转载自:http://hi.baidu.com/lydrainbowcat/blog/item/2 ...

  3. LCA(最近公共祖先)模板

    Tarjan版本 /* gyt Live up to every day */ #pragma comment(linker,"/STACK:1024000000,1024000000&qu ...

  4. CodeVs.1036 商务旅行 ( LCA 最近公共祖先 )

    CodeVs.1036 商务旅行 ( LCA 最近公共祖先 ) 题意分析 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从 ...

  5. LCA近期公共祖先

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

  6. LCA 近期公共祖先 小结

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

  7. HDU 4547 CD操作 (LCA最近公共祖先Tarjan模版)

    CD操作 倍增法  https://i.cnblogs.com/EditPosts.aspx?postid=8605845 Time Limit : 10000/5000ms (Java/Other) ...

  8. D5 LCA 最近公共祖先

    第一题: POJ 1330 Nearest Common Ancestors POJ 1330 这个题可不是以1为根节点,不看题就会一直wa呀: 加一个找根节点的措施: #include<alg ...

  9. LCA 最近公共祖先 Tarjan(离线)算法的基本思路及其算法实现

    首先是最近公共祖先的概念(什么是最近公共祖先?): 在一棵没有环的树上,每个节点肯定有其父亲节点和祖先节点,而最近公共祖先,就是两个节点在这棵树上深度最大的公共的祖先节点. 换句话说,就是两个点在这棵 ...

随机推荐

  1. C C++ ARM X86 函数 方法 的调用约定

    参考:https://bbs.pediy.com/thread-224583.htm 整理成表格方便查询 cdecl(C规范), stdcall(WinAPI默认), fastcall, ATPCS( ...

  2. 多线程状态与优先级、线程同步与Monitor类、死锁

    一.线程状态 二.线程优先级 三.初步尝试多线程 class Program { static void Main(string[] args) { while (true) { MessagePri ...

  3. 利用Git生成本机SSH Key并添加到GitHub中

    本地仓库和github之间是通过SSH加密传输的,所以需要先到github中添加你本机的SSH Key 进行认证. 1.在桌面打开git命令窗口 2.输入“ssh-keygen -t rsa -C   ...

  4. CF1185F Two Pizzas

    CF1185F Two Pizzas 洛谷评测传送门 题目描述 A company of nn friends wants to order exactly two pizzas. It is kno ...

  5. C语言前置知识汇编基础--总线【地址总线,控制总线,数据总线】练习题

    寻址就是用一个数字表示一个字节存储单元的地址,而不是表示这个存储单元中的内容,所以不用乘以8. 1. 1个CPU的寻址能力为8KB,那么它的地址总线宽度是多少? 8*1024=8192     共有8 ...

  6. Ubuntu命令行打开新终端并执行脚本

    gnome-terminal -x bash -c "bash test.sh;exec bash;"

  7. Kafka 入门1

    Kafka 简介 作为一个消息中间件,Kafka 以高扩展性.高吞吐量等特点,在互联网项目中被广泛采用. 不清楚 Kafka 的同学,可以先看看这篇文章: http://blog.csdn.net/s ...

  8. 环境变量对于 VS 有什么用?

    一.前言 环境变量可以用来组织 VS 工程目录.VS 作为一个 IDE,其天职在于帮开发者组织好工程,主要包括对工程中源文件.库文件的组织.(本质上是提供一个可视化的操作界面,让开发者方便的定义编译器 ...

  9. Docker学习4-学会如何让容器开机自启服务

    前言 小龙亲测重启服务器后 docker 容器没跑起来,相信有不少小伙伴在用docker部署容器的时候也发现每次开机服务就没有自启了,需要手动去执行把容器服务开启起来,但有没有可以让它开机自启呢?显然 ...

  10. [LeetCode#180]Consecutive Numbers

    Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...