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. (转)postfix疯狂外发垃圾邮件之分析与解决

    从进程中看到,好像是postfix有问题.我这postfix主要是用来给程序发达邮件用的,如报警,程序外发邮件等.平时postfix进程不会像现在这样异常,这在postf主进程CPU占用高,其它的相关 ...

  2. dirname(__FILE__) === __DIR__

    dirname(__FILE__) === __DIR__get_class($this) == __CLASS__

  3. PuTTY+Xming实现X11的ssh转发

    1 需求分析 有些Linux程序还是不能完全离开窗口环境,或者说离开后操作不方便.其中Oracle就是这样一个程序,其工具程序大多数能够在纯命令行静默执行,如 OCI,DBCA,NetCA等,但是工作 ...

  4. Maltego——互联网情报聚合工具初探(转)

    有时候你可曾想过,从一个Email,或者Twitter,或是网站,甚至姓名等等,能找到一个人千丝万缕的联系,并把这些联系整合,利用起来?Maltego就是这样一款优秀而强大的工具.Maltego允许从 ...

  5. python 回溯法 子集树模板 系列 —— 11、全排列

    问题 实现 'a', 'b', 'c', 'd' 四个元素的全排列. 分析 这个问题可以直接套用排列树模板. 不过本文使用子集树模板.分析如下: 一个解x就是n个元素的一种排列,显然,解x的长度是固定 ...

  6. 设计模式 笔记 抽象工厂模式 Abstract Factory

    //---------------------------15/04/09---------------------------- //Abstract Factory 抽象工厂----对象创建型模式 ...

  7. stl源码剖析 详细学习笔记 算法(4)

    //---------------------------15/03/31---------------------------- //lower_bound(要求有序) template<cl ...

  8. Android与单片机通信常用数据转换方法(汇总)

    下面直接贴代码 1.  将GB2312转化为中文,如BAFAC2DCB2B7→胡萝卜,两个字节合成一个文字 public static String stringToGbk(String string ...

  9. 更改jenkins的默认工作空间并迁移插件和配置数据

    最近刚使用阿里云ECS centos服务器,购买的是40G的系统盘,60G的数据盘. 昨天在查看服务器磁盘空间的时候,偶然发现 /dev/vda1 下面40G的空间已使用17G, 因为服务器才开始使用 ...

  10. highcharts多个Y轴

    http://blog.sina.com.cn/s/blog_6a53599101019qax.html 多个Y轴的实现方法在于把yAxis设置成一个数组,里面的一个对象代表一条Y轴,利用opposi ...