How far away ?

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

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
#include <bits/stdc++.h>
using namespace std ;
const int N = ; typedef pair<int,int> pii ;
#define X first
#define Y second
int n , m ;
int eh[N] , nxt[N<<] , et[N<<] , ew[N<<] , tot ;
int fa[N] , vis[N] , anc[N] ;
int ux[N] , vx[N] , wx[N] ; vector<pii>qry[N] ; void init() { for( int i = ; i <= n ; ++i ) {
qry[i].clear() ;
vis[i] = ;
} tot = ;
memset( eh , - , sizeof eh ) ; } void addedge( int u , int v , int w ) {
et[tot] = v , nxt[tot] = eh[u] , ew[tot] = w , eh[u] = tot++ ;
} int find(int x){ return fa[x] = ( fa[x]==x?x:find(fa[x]));}
void un( int x , int y ) {
x = find(x);
y = find(y);
if(x==y)return ;
fa[y]=x;
}
void dfs1( int u , int pre ) { vis[u] = ;
fa[u] = u ;
for( int i = eh[u] ; ~i ; i = nxt[i] ) {
int v = et[i] ; if( v == pre ) continue ;
dfs1( v , u ) ;
un( u , v );
} int siz = qry[u].size() ;
for( int i = ; i < siz ; ++i ) {
if( vis[qry[u][i].X] ) {
anc[qry[u][i].Y] = find( qry[u][i].X );
}
}
} int dis[N] ; void dfs2( int u , int pre , int d ) {
dis[u] = d ;
for( int i = eh[u] ; ~i ; i = nxt[i] ) {
int v = et[i] ; if( v == pre ) continue ;
dfs2( v , u , d + ew[i] ) ;
}
} int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int _ ; scanf("%d",&_) ;
while( _-- ) { scanf("%d%d",&n,&m) ;
init() ;
for( int i = ; i < n ; ++i ) {
int u , v , w ;
scanf("%d%d%d",&u,&v,&w) ;
addedge( u , v , w ) ;
addedge( v , u , w ) ;
} for( int i = ; i < m ; ++i ) {
scanf("%d%d",&ux[i],&vx[i]) ;
qry[ ux[i] ].push_back( pii( vx[i] , i ) ) ;
qry[ vx[i] ].push_back( pii( ux[i] , i ) ) ;
} dfs1( , ) ;
dfs2( , , ) ; for( int i = ; i < m ; ++i ) {
printf("%d\n",dis[ux[i]]+dis[vx[i]]-*dis[anc[i]]);
}
}
return ;
}

hdu 2586 How far away ? ( 离线 LCA , tarjan )的更多相关文章

  1. hihoCoder #1067 : 最近公共祖先·二 [ 离线LCA tarjan ]

    传送门: #1067 : 最近公共祖先·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上上回说到,小Hi和小Ho用非常拙劣——或者说粗糙的手段山寨出了一个神奇的网站 ...

  2. poj1470 Closest Common Ancestors [ 离线LCA tarjan ]

    传送门 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 14915   Ac ...

  3. HDU 2586.How far away ?-离线LCA(Tarjan)

    2586.How far away ? 这个题以前写过在线LCA(ST)的,HDU2586.How far away ?-在线LCA(ST) 现在贴一个离线Tarjan版的 代码: //A-HDU25 ...

  4. hdu 2586 How far away?(LCA模板题+离线tarjan算法)

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

  5. 【HDU 2586 How far away?】LCA问题 Tarjan算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵n个节点的无根树,每条边有各自的权值.给出m个查询,对于每条查询返回节点u到v的最 ...

  6. HDU 2586 How far away ? (LCA,Tarjan, spfa)

    题意:给定N个节点一棵树,现在要求询问任意两点之间的简单路径的距离,其实也就是最短路径距离. 析:用LCA问题的Tarjan算法,利用并查集的优越性,产生把所有的点都储存下来,然后把所有的询问也储存下 ...

  7. HDU 2586 How far away ? (LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 LCA模版题. RMQ+LCA: #include <iostream> #incl ...

  8. Hdu 2586 树链剖分求LCA

    Code: #include<cstdio> #include<cstring> #include<vector> #include<algorithm> ...

  9. hdu 2586 How far away? (LCA模板)

    题意: N个点,形成一棵树,边有长度. M个询问,每个询问(a,b),询问a和b的距离 思路: 模板题,看代码.DFS预处理算出每个结点离根结点的距离. 注意: qhead[maxn],而不是qhea ...

随机推荐

  1. xor or and 线段树

    每一位维护一颗线段树 (-1)^1 =-2 (-2)^1=-1 #include <cstdio> #include<iostream> using namespace std ...

  2. Hive函数介绍

    一些函数不太会,查了些资料,分享一下 Hive已定义函数介绍: 1.字符串长度函数:length 语法: length(string A)返回值: int举例:[sql] view plain cop ...

  3. NOIP2018复赛游记

    Day -oo (这里是负无穷啦qwq) 为了NOIP2018,我已经准备了好久 暑假的封闭式训练,国庆后停掉了晚自习,距NOIP一周时更是停掉了全天的课...... 我像是一个圆,在近乎无限的空间里 ...

  4. substring()方法是如何工作

    1.substring()方法做了什么? substring(beginIndex,endIndex)方法返回一个从beginIndex到endIndex-1的字符串 String x = " ...

  5. pymysql(一)检索、增加、更新、删除数据

    (一)  SELECT 检索数据 代码如下: import pymysql '''pymysql使用指南host = '127.0.0.1'回送地址,指本地机port = 3306MySQL的默认端口 ...

  6. 嵌入式Linux之NFS配置

    NFS(Network File System) 1.RPC和rpcbind RPC(Remote Procedure Call)即远程过程调用,是分布式应用的基础,即允许计算机远程调用网络上其他计算 ...

  7. k8s编辑pod配置信息

    kubectl edit deployment devops-service -n c7n-system

  8. iOS SDK开发之 .a静态库

    查看.framework静态库的生成及使用单击此处 注:这篇教程将只使用一小部分Objective-C代码,本文主要讲解从开始到应用的详细步骤.环境:xcode 9.2下面我们开始操作: 第一步:创建 ...

  9. Mac上的应用,例如Xcode需要输入原始下载账号才能更新问题

    为了免下载安装Xcode,安装时使用了别人提供的Xcode.dmg安装,或者公司接管上任同事使用的Mac时,上面的应用都是用别人的账号购买下载的,而非使用自己账号在AppStore下载的. 这样的安装 ...

  10. QT Desinger设计窗体应用程序框架

    目录 目录 前言 系统软件 QT Designer Using QT Designer Open QTDesigner Tool Widget Box QT Designer的布局 属性栏 示例 i ...