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 day - 8 文件

    文件的相关操作 1.文件的两种路径 绝对路径:需要从根目录下一层一层往下去找,文件或者程序所在的地方,中间所经过的所有的路径到你要找的文件或程序,就是绝对路径. 相对路径:只需要将要找的文件或者程序, ...

  2. android studio Error:Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Request"

    android studio运行会遇到Error:Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Reques ...

  3. sql server filter table name

    https://stackoverflow.com/questions/26577464/how-to-find-a-table-in-sql-server-if-only-the-partial-t ...

  4. python数据分组运算

    摘要: pandas 的 GroupBy 功能可以方便地对数据进行分组.应用函数.转换和聚合等操作.   # 原作者:lionets GroupBy 分组运算有时也被称为 “split-apply-c ...

  5. codeforces 673A A. Bear and Game(水题)

    题目链接: A. Bear and Game time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  6. AFNetworking源码的学习

    忽略编译警告 AFNetworking源码中常常会出现忽略警告的代码,如下: 1 2 3 4 #pragma clang diagnostic push #pragma clang diagnosti ...

  7. 【HDU 4722】 Good Numbers

    [题目链接] 点击打开链接 [算法] f[i][j]表示第i位,数位和对10取模余j的数的个数 状态转移,计算答案都比较简单,笔者不再赘述 [代码] #include<bits/stdc++.h ...

  8. struts2通用标签

    Struts2框架为我们提供了很多标签,这些标签总体上可以分为两类:通用标签和UI标签.通用标签分为两类:数据标签和控制标签.数据标签用于访问值栈中数据,控制标签用于控制呈现页面时数据执行流程.使用S ...

  9. (转)Excel自定义格式详解

    ”G/通用格式”:以常规的数字显示,相当于”分类”列表中的”常规”选项.例:代码:”G/通用格式”.10显示为10:10.1显示为10.1. 2. “#”:数字占位符.只显有意义的零而不显示无意义的零 ...

  10. 利用高德地图javascriptAPI做一个自己的地图

    最近由于项目中需要制作一个地图,用来选择活动地点,我就花了两天利用高德地图的javascriptAPI自制了一个地图的demo.在这了记录一下我学习的过程. 一.进入高德地图官网,再找到高德地图的开放 ...