hdu 2586 How far away ? ( 离线 LCA , tarjan )
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
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.
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.
3 2
1 2 10
3 1 15
1 2
2 3
2 2
1 2 100
1 2
2 1
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 )的更多相关文章
- hihoCoder #1067 : 最近公共祖先·二 [ 离线LCA tarjan ]
传送门: #1067 : 最近公共祖先·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上上回说到,小Hi和小Ho用非常拙劣——或者说粗糙的手段山寨出了一个神奇的网站 ...
- poj1470 Closest Common Ancestors [ 离线LCA tarjan ]
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 14915 Ac ...
- HDU 2586.How far away ?-离线LCA(Tarjan)
2586.How far away ? 这个题以前写过在线LCA(ST)的,HDU2586.How far away ?-在线LCA(ST) 现在贴一个离线Tarjan版的 代码: //A-HDU25 ...
- 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 ...
- 【HDU 2586 How far away?】LCA问题 Tarjan算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵n个节点的无根树,每条边有各自的权值.给出m个查询,对于每条查询返回节点u到v的最 ...
- HDU 2586 How far away ? (LCA,Tarjan, spfa)
题意:给定N个节点一棵树,现在要求询问任意两点之间的简单路径的距离,其实也就是最短路径距离. 析:用LCA问题的Tarjan算法,利用并查集的优越性,产生把所有的点都储存下来,然后把所有的询问也储存下 ...
- HDU 2586 How far away ? (LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 LCA模版题. RMQ+LCA: #include <iostream> #incl ...
- Hdu 2586 树链剖分求LCA
Code: #include<cstdio> #include<cstring> #include<vector> #include<algorithm> ...
- hdu 2586 How far away? (LCA模板)
题意: N个点,形成一棵树,边有长度. M个询问,每个询问(a,b),询问a和b的距离 思路: 模板题,看代码.DFS预处理算出每个结点离根结点的距离. 注意: qhead[maxn],而不是qhea ...
随机推荐
- Missing radix parameter.报错解决方法
当报“Missing radix parameter.”这个错的时候,是因为使用parseInt没有传第二个参数,以前简写的时候大家几乎都不传,甚至不知道还有第二个参数. 当时候Eslint预发检查时 ...
- jQuery字体图标的三种方法
BootStrap框架原生图标 在导入BootStrap包的同时,导入bootstrap-3.3.7-dist/css/bootstrap.css层叠样式; <button type=" ...
- socket编程相关阐述
一.socket初识 ①服务端 import socket server = socket.socket() server.bind(('127.0.0.1', 8080)) server.liste ...
- 搭建私有git仓库gogs
安装 gogs 下载 gogs download 安装 解压压缩包. 使用命令 cd 进入到刚刚创建的目录. 执行命令 ./gogs web,然后,就没有然后了. #后台运行 $ nohup ./go ...
- 极验验证码在php5.6.27下不显示
PHP5.6需要改php.ini 去掉;always_populate_raw_post_data = -1的 :
- csc.exeCPU100%
可以通过nuget把Microsoft.CodeDom.Providers.DotNetCompilerPlatform和Microsoft.Net.Compilers这两个包卸载
- LibUsbDotNet使用方法
最近在用C#调试USB程序,libusb源码是C语言的,C#用起来不方便,偶然在网上看到了LibUsbDotNet,这是开源的项目,下载后参考Example,用起来非常方便. LibUsbDotNet ...
- 搜索引擎算法研究专题五:TF-IDF详解
搜索引擎算法研究专题五:TF-IDF详解 2017年12月19日 ⁄ 搜索技术 ⁄ 共 1396字 ⁄ 字号 小 中 大 ⁄ 评论关闭 TF-IDF(term frequency–inverse ...
- MFC ATL STL概要
MFC-----应用程序框架 ATL-----写COM的利器 STL-----用来写逻辑部分 MFC: MFC的目标是桌面应用,当然也有网络部分但很不充分.MFC是一套APP ...
- C# 打印倒三角
void test6(int num) { try { #region 方法1 int maxstar = (num - 1) * 2 + 1; string line = ""; ...