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 bu 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 areato 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
解题思路:Tarjan算法求树上任意两点的最短路。
AC代码:
 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=;
const int maxv=;
int T,n,m,x,y,z,fa[maxn],dist[maxn],ans[maxv];bool vis[maxn];
vector<pair<int,int> > tree[maxn],query[maxn];
void init(){
for(int i=;i<=n;++i)fa[i]=i,tree[i].clear(),query[i].clear();
memset(vis,false,sizeof(vis));
memset(dist,,sizeof(dist));
memset(ans,,sizeof(ans));
}
int query_father(int x){
int per=x,tmp;
while(fa[per]!=per)per=fa[per];
while(x!=per){tmp=fa[x];fa[x]=per;x=tmp;}//路径压缩
return x;
}
void unite(int x,int y){
x=query_father(x),y=query_father(y);
if(x!=y)fa[y]=x;
}
void Tarjan_LCA(int cur,int val){
vis[cur]=true;///先标记为true,这样如果同一支树上前面的祖先节点已访问,但是还没有归并到集合中去相当于为其本身,所以对下面计算两个节点的最近距离是没有影响的
dist[cur]=val;
for(size_t i=;i<tree[cur].size();++i){
int v=tree[cur][i].first;
int w=tree[cur][i].second;
if(!vis[v]){
Tarjan_LCA(v,val+w);///先序遍历
unite(cur,v);///回退后续遍历归并集合
}
}
for(size_t i=;i<query[cur].size();++i){///同时扫描与cur有关的点对查询
int to=query[cur][i].first;
int id=query[cur][i].second;
if(vis[to])ans[id]=dist[cur]+dist[to]-*dist[query_father(to)];
}
}
int main(){
while(~scanf("%d",&T)){
while(T--){
scanf("%d%d",&n,&m);
init();
for(int i=;i<n;++i){
scanf("%d%d%d",&x,&y,&z);
tree[x].push_back(make_pair(y,z));
tree[y].push_back(make_pair(x,z));
}
for(int i=;i<=m;++i){
scanf("%d%d",&x,&y);
query[x].push_back(make_pair(y,i));
query[y].push_back(make_pair(x,i));
}
Tarjan_LCA(,);
for(int i=;i<=m;++i)printf("%d\n",ans[i]);
}
}
return ;
}

LCA最近公共祖先知识点整理的更多相关文章

  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. LCA最近公共祖先 ST+RMQ在线算法

    对于一类题目,是一棵树或者森林,有多次查询,求2点间的距离,可以用LCA来解决.     这一类的问题有2中解决方法.第一种就是tarjan的离线算法,还有一中是基于ST算法的在线算法.复杂度都是O( ...

  8. Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)【转】【修改】

    一.基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成 ...

  9. (转)Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)

    基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个 ...

随机推荐

  1. Python开发【第3节】【Python分支结构与循环结构】

    1.流程控制  流程: 计算机执行代码的顺序就是流程 流程控制: 对计算机代码执行顺序的管理就是流程控制 流程分类: 流程控制共分为3类: 顺序结构 分支结构/选择结构 循环结构 2.分支结构(if. ...

  2. 智能停车O2O 独角兽初现:“ETCP停车”获5000万美金A轮融资

        日前,国内第一智能停车平台"ETCP停车"宣布完毕A轮融资,由源代码资本.SIG.易车网.经纬中国和商企界知名人士联合投资超过5000万美金.同一时候获悉,ETCP作为中国 ...

  3. 链式mapreduce

    在hadoop 中一个Job中可以按顺序运行多个mapper对数据进行前期的处理,再进行reduce,经reduce后的结果可经个经多个按顺序执行的mapper进行后期的处理,这样的Job是不会保存中 ...

  4. Immutable学习及 React 中的实践

    为什么用immutable.js呢.有了immutable.js可以大大提升react的性能. JavaScript 中的对象一般是可变的(Mutable),因为使用了引用赋值,新的对象简单的引用了原 ...

  5. 20170218-取值Domain

    1.通过视图DD07V, 例子:取SD 凭证类别的DOMAIN(VBTYP)DATA: LT_DD07V TYPE TABLE OF DD07V.SELECT * INTO T_DD07V FROM ...

  6. swift的String处理

    import UIKit import CoreText class ViewController: UIViewController { override func viewDidLoad() { ...

  7. include <ctype.h> 头文件包含函数总结

    里面包含的函数主要是: 1.字符测试函数,函数原型一般为:int isXXXX( int ); 参数为int, 只能正确处理[0, 127]. 2.字符映射函数,函数原型一般为:int toXXXX( ...

  8. java 读写Oracle Blob字段

    许久没有分享代码了,把这段时间写的一个Java操作Blob字段,有日子没写Java了,就当作笔记记录一下.1. [代码][Java]代码     跳至 [1] [全屏预览]package com.wa ...

  9. hel软工网络16个人作业1

    1Task1:注册个人博客账号 1Task2:注册码云账号 1Task3:提出问题 3.1问题一:软件工程是什么? 在第一章中我们可以从P8得到: 1.软件工程就是把系统的.有序的.可量化的方法应用到 ...

  10. Ubuntu上命令行下卸载软件

    sudo apt-get --purge remove 软件名 (加了--purge表示会删除配置) sudo apt-get autoremove (这个命令后面文章有解释) dpkg -l (查看 ...