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 ...
随机推荐
- node.js入门学习(五)--Demo模块化改造
1.node.js中模块的分类 1)node.js内置模块(核心,原生) 所有内置模块在安装node.js时就已经编译成二进制文件,可以直接加载运行(速度较快),部分内置模块,在node.exe这个进 ...
- CSS无图片三角形
border:6px solid #f2f2f2; border-color:#999 transparent transparent transparent; border-style:solid ...
- IDEA中Springboot静态文件加载(热部署)
Springboot项目静态文件加载 昨天写项目的时候碰到一个问题,就是静态文件css无法读取到项目中,我仔细思考了下,总结了下,可能有两个问题 1.页面未加载更新 这个可能性非常大,Chrome就是 ...
- 小程序的image图片显示
最近接触到了一点小程序的东西,跟我们平常的HTML还真不太一样,这里我先大概讲一下图片的显示问题, 小程序的图片用的是<image><image/>标签,他默认的大小是宽300 ...
- 【技术分享:python 应用之一】如何使用 Python 对 Excel 做一份数据透视表
客户这边,其中有一张如同上图所示的数据汇总表,然而需求是,需要将这张表数据做一个数据透视表,最后通过数据透视表中的数据,填写至系统数据库.拿到需求,首先就想到肯定不能直接用设计器去操作 Excel,通 ...
- Proxy-代理器(预计vue3.0实现双向绑定的方式)
todo 常见的基于数据劫持的双向绑定有两种实现,一个是目前Vue在用的Object.defineProperty,另一个是ES2015中新增的Proxy,而Vue的作者宣称将在Vue3.0版本后加入 ...
- [CSP-S模拟测试]:山屋惊魂(模拟)
题目传送门(内部题90) 输入格式 前四行依次表示每种属性:$Might$.$Speed$.$Sanity$.$Knowledge$.每行一个$8$位数表示该属性的$8$个档的值,第二个数表示初始在哪 ...
- 京东面试题:Java中 ++i 的操作是线程安全的么?为什么?如何使其线程安全呢?
你真的了解volatile关键字吗?http://blog.csdn.net/FansUnion/article/details/79495080 面试题:为什么最后两行没有运行?http://blo ...
- 第三周课程总结&实验报告(一)
实验报告(一) 1.打印输出所有的"水仙花数",所谓"水仙花数"是指一个3位数,其中各位数字立方和等于该数本身.例如,153是一个"水仙花数" ...
- 搜索引擎算法研究专题五:TF-IDF详解
搜索引擎算法研究专题五:TF-IDF详解 2017年12月19日 ⁄ 搜索技术 ⁄ 共 1396字 ⁄ 字号 小 中 大 ⁄ 评论关闭 TF-IDF(term frequency–inverse ...