How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20971    Accepted Submission(s): 8245

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模板题,Tarjan还是比较好理解的。。。
推荐一篇很不错的博客:
https://www.cnblogs.com/JVxie/p/4854719.html
 
实现代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = 1e5+; struct node{
int to,next,w,sum;
};
int n,m,cnt1,cnt2;
node e[M<<]; //双向边
node q[M]; //询问的边
int fa[M],head[M<<],qhead[M];
bool vis[M];
ll d[M],res[M]; int find(int x){
return fa[x] == x?x:fa[x] = find(fa[x]);
} void add(int u,int v,int w){
e[++cnt1].w=w;e[cnt1].to=v;e[cnt1].next=head[u];head[u]=cnt1;
e[++cnt1].w=w;e[cnt1].to=u;e[cnt1].next=head[v];head[v]=cnt1;
} void add1(int u,int v){
q[++cnt2].to=v;q[cnt2].next=qhead[u];qhead[u]=cnt2;
} void dfs(int u,int fa,ll w){
d[u] = w;
for(int i = head[u];i!=-;i=e[i].next){
int v = e[i].to;
if(v==fa) continue;
dfs(v,u,w+e[i].w);
}
} void tarjan(int u){
fa[u] = u; vis[u] = ;
for(int i = head[u];i!=-;i=e[i].next){
int v = e[i].to;
if(!vis[v]){
tarjan(v);
fa[v] = u;
}
}
for(int i = qhead[u];i!=-;i=q[i].next){
int v = q[i].to;
if(vis[v]){
q[i].sum = find(v);
res[i] = d[u] + d[v] - *d[q[i].sum];//两者的距离
//cout<<i<<" "<<res[i]<<endl;
}
}
} void solve(){
for(int i = ;i < n;i ++) fa[i] = i;
memset(head,-,sizeof(head)); memset(qhead,-,sizeof(qhead));
memset(vis,,sizeof(vis));
cnt1 = cnt2 = ;
int u,v,w;
for(int i = ;i < n;i ++){
cin>>u>>v>>w;
add(u,v,w);
}
for(int i = ;i < m;i ++){
cin>>u>>v;
add1(u,v);
}
dfs(,-,);
tarjan();
} int main()
{
int t;
ios::sync_with_stdio();
cin.tie();cout.tie();
cin>>t;
while(t--){
cin>>n>>m;
solve();
for(int i = ;i <= m;i ++){
cout<<res[i]<<endl;
}
}
return ;
}

hdu 2586 How far away ?(LCA - Tarjan算法 离线 模板题)的更多相关文章

  1. Tarjan算法离线 求 LCA(最近公共祖先)

    本文是网络资料整理或部分转载或部分原创,参考文章如下: https://www.cnblogs.com/JVxie/p/4854719.html http://blog.csdn.net/ywcpig ...

  2. 最近公共祖先LCA(Tarjan算法)的思考和算法实现

    LCA 最近公共祖先 Tarjan(离线)算法的基本思路及其算法实现 小广告:METO CODE 安溪一中信息学在线评测系统(OJ) //由于这是第一篇博客..有点瑕疵...比如我把false写成了f ...

  3. 最近公共祖先LCA(Tarjan算法)的思考和算法实现——转载自Vendetta Blogs

    LCA 最近公共祖先 Tarjan(离线)算法的基本思路及其算法实现 小广告:METO CODE 安溪一中信息学在线评测系统(OJ) //由于这是第一篇博客..有点瑕疵...比如我把false写成了f ...

  4. [CF 191C]Fools and Roads[LCA Tarjan算法][LCA 与 RMQ问题的转化][LCA ST算法]

    参考: 1. 郭华阳 - 算法合集之<RMQ与LCA问题>. 讲得很清楚! 2. http://www.cnblogs.com/lazycal/archive/2012/08/11/263 ...

  5. POJ 1330 Nearest Common Ancestors(LCA Tarjan算法)

    题目链接:http://poj.org/problem?id=1330 题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先. 数据范围:n [2, 10000] 思路:从 ...

  6. LCA:Tarjan算法实现

    本博文转自http://www.cnblogs.com/JVxie/p/4854719.html,转载请注明出处 首先是最近公共祖先的概念(什么是最近公共祖先?): 在一棵没有环的树上,每个节点肯定有 ...

  7. Tarjan 算法求 LCA / Tarjan 算法求强连通分量

    [时光蒸汽喵带你做专题]最近公共祖先 LCA (Lowest Common Ancestors)_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili tarjan LCA - YouTube Tarj ...

  8. HDU 2874 Connections between cities(LCA Tarjan)

    Connections between cities [题目链接]Connections between cities [题目类型]LCA Tarjan &题意: 输入一个森林,总节点不超过N ...

  9. 有向图强连通分量的Tarjan算法及模板

    [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强联通(strongly connected),如果有向图G的每两个顶点都强联通,称有向图G是一个强联通图.非强联通图有向 ...

随机推荐

  1. HDU 2647 Reward(拓扑排序,vector实现邻接表)

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  2. phpstorm开发环境搭建流程

    1.下载phpstorm 2.网上找注册码 phpstorm 8 license key Learn Programming===== LICENSE BEGIN =====63758-1204201 ...

  3. Debuggex – 超好用的正则表达式可视化调试工具

    正则表达式通常被用来检索或替换符合某个模式的文本内容,编写正则是开发人员的必备技能.简单的正则表达式一下就能看懂含义,但是复杂的正则理解起来就很困难了.有了这款可视化的正则调试工具,以后编写正则表达式 ...

  4. 【本地服务器】windows下nginx安装操作教程

    1.下载nginx 下载地址:    (可选择下载 Stable version 版本) 2.把安装放到C盘或其他盘的根目录,并解压文件压缩包,可以重命名解压的文件夹,方便找到路径 (注意不要直接双击 ...

  5. 20155239吕宇轩《网络对抗》Exp3 免杀原理与实践

    20155239吕宇轩<网络对抗>Exp3 免杀原理与实践 实验过程 Kali使用上次实验msfvenom产生后门的可执行文件,上传到老师提供的网址http://www.virscan.o ...

  6. 20155302《网络对抗》Exp8 Web基础

    20155302<网络对抗>Exp8 Web基础 实验内容 (1).Web前端HTML(0.5分) 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个 ...

  7. 【来龙去脉系列】AutoMapper一款自动映射框架

    前言 通常在一个应用程序中,我们开发人员会在两个不同的类型对象之间传输数据,通常我们会用DTOs(数据传输对象),View Models(视图模型),或者直接是一些从一个service或者Web AP ...

  8. POJ 2388&&2299

    排序(水题)专题,毕竟如果只排序不进行任何操作都是极其简单的. 事实上,排序算法十分常用,在各类高级的算法中往往扮演着一个辅助的部分. 它看上去很普通,但实际的作用却很大.许多算法在失去排序后将会无法 ...

  9. Caffe 深度学习框架上手教程

    Caffe 深度学习框架上手教程   blink 15年1月   Caffe (CNN, deep learning) 介绍 Caffe -----------Convolution Architec ...

  10. 【转载】VS中的路径宏 vc++中OutDir、ProjectDir、SolutionDir各种路径

    原文:http://www.cnblogs.com/lidabo/archive/2012/05/29/2524170.html 说明 $(RemoteMachine) 设置为“调试”属性页上“远程计 ...