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. (转)公有云vr客户端tcp连接数太多造成 系统卡顿问题 [bittorrent tracker优化] -公有云常见网络问题及思路

    在公有云服务器 发现使用tcp(http)的tracker连接数太多 用户太多会造成windows系统卡顿 特此发表一下修改配置和路由器的方法 解决卡顿问题 解决方法1(参考内容): 修改 /etc/ ...

  2. 3D Touch开发技巧的笔记

    iPhone6s以及iPhone6s plus搭载iOS9,有一个新功能叫做3D Touch,这个功能有很大的用处,关键是要会用,这给交互方式又多了一个新的选择和思考,比如说游戏中的额外控制选项.绘图 ...

  3. 20155325 Exp3 免杀原理与实践

    基础问题回答 杀软是如何检测出恶意代码的? 1.1 基于特征码的检测 1.1.1 特征库举例-Snort 1.2 启发式恶意软件检测 1.3 基于行为的恶意软件检测 免杀是做什么? 一般是对恶意软件做 ...

  4. 20155331《网路对抗》Exp8 WEB基础实践

    20155331<网路对抗>Exp8 WEB基础实践 基础问题回答 什么是表单 表单在网页中主要负责数据采集功能.一个表单有三个基本组成部分: 表单标签,这里面包含了处理表单数据所用CGI ...

  5. 20155334 曹翔 《网络对抗》逆向及Bof基础

    20155334 曹翔 <网络对抗>逆向及Bof基础 实践目标: 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回 ...

  6. arm学习之汇编跳转指令总结

    目前所知道的跳转指令有 b,bl,bep,bne.他们共同点是都是以b开头,首先从字面上分析:b:是Branch,表示分支.bl:是Branch Link表示带连接的分支.bep:Branch ,Eq ...

  7. mysql基础(一)——表、索引、视图

    SQL语句不区分大小写 创建数据库 create database myData 删除数据库 drop database myData 创建表 create table company ( code ...

  8. 【亲测有效】Nodepad++/Sublime Text3中Python脚本运行出现语法错误:IndentationError: unindent does not match any outer indentation level解决策略

    我在开发游戏的时候,发现一个python脚本,本来都运行好好的,然后写了几行代码,而且也都确保每行都对齐了,但是运行的时候,却出现语法错误: IndentationError: unindent do ...

  9. 关于java线程池的一丢丢

    线程池应用达到的目的 1.降低资源消耗:可以重复利用已创建的线程从而降低线程创建和销毁所带来的消耗. 2.提高响应速度:当任务到达时,不需要等线程创建就可以立即执行. 3.提高线程的可管理性:使用线程 ...

  10. win10引导错误的修复(内容系转载)

    #!尊重原作者,再此声明此内容属于网络转载,只是为了能保留下来方便日后查阅!!! win10误删引导文件,0xc0000098的解决方案,bcd引导文件受损情况分析 一.※相对简单的解决方法,对应的情 ...