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. 每日五题(Spring)

    1使用Spring框架的优点是什么? 控制反转: Spring通过控制反转实现了松散耦合,对象们给出它们的依赖,而不是创建或查找依赖的对象们. 面向切面的编程(AOP): Spring支持面向切面的编 ...

  2. 【翻译自mos文章】即使resource_limit = false, password的 资源限制也会生效

    即使resource_limit = false, password的 资源限制也会生效 參考原文: Resource limits for passwords work even with reso ...

  3. javascript数据基本类型和引用类型

    JavaScript基本数据类型: js基本数据类型包括:undefined,null,number,boolean,string.基本数据类型是按值访问的,就是说我们可以操作保存在变量中的实际的值. ...

  4. eclipse安装lombok和常用注解使用

    1.下载lombok.jar lombok 的官方网址:http://projectlombok.org/   2.运行lombok.jar: java -jar  D:\eclipse-luna\l ...

  5. MySQL数据库设计常犯的错以及对性能的影响

    1.过分的反范式化为表建立太多的列 我们在设计数据库的结构时,比较容易犯的第一个错误就是对表进行了过分的反范式化的设计,这就容易造成了表中的列过多,虽然说Mysql允许为一个表建立很多的列,但是由于M ...

  6. kafka条件查询excel拼接

    1 SELECT COUNT(*) FROM wiseweb_crawler_metasearch_page20171214 WHERE (content like '%内蒙古%'or content ...

  7. IOS中的沙盒机制

    IOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序只能在为该应用创建的文件夹内读取文件,不可以访问其他地方的内容.所有的非代码文件都保存在这个地方,比如图片.声音.属性列表和文本文件 ...

  8. MP4V2库与MP4AV库编译

    最近在开发一个将RTP流存储为MP4文件的功能.其中针对MP4文件读写,用到了两个开源的库.其中MP4V2用于数据的读写,MP4AV用于对其中的数据帧进行分析. MP4V2和MP4AV都是开源项目MP ...

  9. 孙鑫C++教学视频

    视频百度云:https://pan.baidu.com/s/1jKf6GoY 在线观看:http://list.youku.com/albumlist/show?id=3567028&asce ...

  10. Gulp安装及配合组件构建前端开发一体化(转)

    Gulp安装及配合组件构建前端开发一体化 所有功能前提需要安装nodejs(本人安装版本v0.10.26)和ruby(本人安装版本1.9.3p484). Gulp 是一款基于任务的设计模式的自动化工具 ...