How far away ?

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

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
Source
LCA离线做法的板子  Tarjan算法(DFS+并查集)
不想解释  我就想贴个板子
#include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
#include<cstdlib>
#include<vector>
#include<set>
#include<queue>
#include<cstring>
#include<string.h>
#include<algorithm>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=+;
int head[N];
int vis[N];
int parent[N];
int ans[N];
int cnt;
vector<int>q[N];
vector<int>Q[N];
int dis[N];
int n;
int ancestor[N];
struct node{
int to,next,w;
}edge[*N+];
void init(){
cnt=;
memset(ans,,sizeof(ans));
memset(dis,,sizeof(dis));
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++){
parent[i]=i;
Q[i].clear();
q[i].clear();
}
}
void add(int u,int v,int w){
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int find(int x){
int r=x;
while(r!=parent[x])r=parent[r];
int i=x;
int j;
while(parent[i]!=r){
j=parent[i];
parent[i]=r;
i=j;
}
return r;
}
void Union(int x,int y){
x=find(x);
y=find(y);
if(x!=y)parent[y]=x;
}
void Tarjan(int x,int w){
vis[x]=;
dis[x]=w;
//cout<<dis[x]<<endl;
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(vis[v])continue;
Tarjan(v,w+edge[i].w);
Union(x,v);
ancestor[find(x)]=x;
}
for(int i=;i<q[x].size();i++){
int u=q[x][i];
if(vis[u]){
int t=ancestor[find(u)];
ans[Q[x][i]]=dis[x]+dis[u]-dis[t]*;
}
}
}
int main(){
int m;
int t;
scanf("%d",&t);
while(t--){
init();
scanf("%d%d",&n,&m);
int u,v,w;
for(int i=;i<n-;i++){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
for(int i=;i<m;i++){
scanf("%d%d",&u,&v);
q[u].push_back(v);
q[v].push_back(u);
Q[u].push_back(i);
Q[v].push_back(i);
}
Tarjan(,);
// for(int i=1;i<=n;i++)cout<<dis[i]<<" ";
//cout<<endl;
for(int i=;i<m;i++){
// cout<<4<<endl;
cout<<ans[i]<<endl;
}
}
}
 

hdu 2586(Tarjan 离线算法)的更多相关文章

  1. HDU 2586 /// tarjan离线求树上两点的LCA

    题目大意: 询问一棵树里 u 到 v 的距离 可由 dis[ u到根 ] + dis[ v到根 ] - 2*dis[ lca(u,v) ] 得到 https://blog.csdn.net/csyzc ...

  2. LCA(最近公共祖先)--tarjan离线算法 hdu 2586

    HDU 2586 How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  3. LCA问题的ST,tarjan离线算法解法

    一  ST算法与LCA 介绍 第一次算法笔记这样的东西,以前学算法只是笔上画画写写,理解了下,刷几道题,其实都没深入理解,以后遇到新的算法要把自己的理解想法写下来,方便日后回顾嘛>=< R ...

  4. 最近公共祖先LCA Tarjan 离线算法

    [简介] 解决LCA问题的Tarjan算法利用并查集在一次DFS(深度优先遍历)中完成所有询问.换句话说,要所有询问都读入后才开始计算,所以是一种离线的算法. [原理] 先来看这样一个性质:当两个节点 ...

  5. LCA 最近公共祖先 Tarjan(离线)算法的基本思路及其算法实现

    首先是最近公共祖先的概念(什么是最近公共祖先?): 在一棵没有环的树上,每个节点肯定有其父亲节点和祖先节点,而最近公共祖先,就是两个节点在这棵树上深度最大的公共的祖先节点. 换句话说,就是两个点在这棵 ...

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

    这篇博客对Tarjan算法的原理和过程模拟的很详细. 转载大佬的博客https://www.cnblogs.com/JVxie/p/4854719.html 第二次更新,之前转载的博客虽然胜在详细,但 ...

  7. HDU-2586-How far away(LCA Tarjan离线算法)

    链接:https://vjudge.net/problem/HDU-2586 题意: 勇气小镇是一个有着n个房屋的小镇,为什么把它叫做勇气小镇呢,这个故事就要从勇气小镇成立的那天说起了,修建小镇的时候 ...

  8. POJ 1330 Nearest Common Ancestors 【最近公共祖先LCA算法+Tarjan离线算法】

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20715   Accept ...

  9. HDU ACM 2586 How far away ?LCA-&gt;并查集+Tarjan(离线)算法

    题意:一个村子有n个房子,他们用n-1条路连接起来,每两个房子之间的距离为w.有m次询问,每次询问房子a,b之间的距离是多少. 分析:近期公共祖先问题,建一棵树,求出每一点i到树根的距离d[i],每次 ...

随机推荐

  1. C# 字符串的入门

    1."@"表示字符串中的"\"不当成转义符. 2.转义符或者特殊处理的一些字符只是针对于代码中直接写出的字符串中,对于程序运行中读取出来的转义符或者特殊处理的字 ...

  2. java中“53”个关键字(含2个保留字)

    1.java的关键字(keyword)有多少个? 51+2个保留字=53个关键字(java的关键字都是小写的!!) 2.java的保留字(reserve word)有多少个?问题:分别是什么? 2个保 ...

  3. PHP 之二位数组根据某个字段排序封装

    /** * @param $array * @param $keys * @param string $sort * @return array */ function arraySort($arra ...

  4. centos设置ssh安全只允许用户从指定的IP登陆

    1.编辑文件 /etc/ssh/sshd_config vi /etc/ssh/sshd_config 2.root用户只允许在如下ip登录 AllowUsers root@203.212.4.117 ...

  5. B+树知识点

    B+树介绍   目录 B+树 B+树的插入操作 B+树的删除操作 回到顶部 B+树 B+树和二叉树.平衡二叉树一样,都是经典的数据结构.B+树由B树和索引顺序访问方法(ISAM,是不是很熟悉?对,这也 ...

  6. Quartz.NET 定时任务使用

    class Program { static void Main(string[] args) { StartJob(); Console.ReadKey(); } static void Start ...

  7. ubuntu18.04安装chrome浏览器

    前几天把系统弄崩溃了,弄了好久也没弄好,索性直接装18.04,下面是安装chrom浏览器的步骤,网络上照着16.04安装的,应该是一样的 启动终端. 输入以下命令: sudo wget http:// ...

  8. -------------Django-----URLS路由

    一.相约Django. 1.Django的特点:Django定义了服务分布.路由映射.模板编程.数据处理的一套完整的功能. (1)集成数据访问组件:Django的model层自带数据库ORM组件. ( ...

  9. NLTK学习笔记(七):文本信息提取

    目录 实体识别:分块技术 分块语法的构建 树状图 IOB标记 开发和评估分块器 命名实体识别和信息提取 如何构建一个系统,用于从非结构化的文本中提取结构化的信息和数据?哪些方法使用这类行为?哪些语料库 ...

  10. 【codeforces 792C】Divide by Three

    [题目链接]:http://codeforces.com/contest/792/problem/C [题意] 让你删掉最少的数字使得剩下的数字%3==0 [题解] 看代码..内置题解了现在. [完整 ...