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 ...
随机推荐
- spring自定义注解实现登陆拦截器
1.spring自定义注解实现登陆拦截器 原理:定义一个注解和一个拦截器,拦截器拦截所有方法请求,判断该方法有没有该注解.没有,放行:有,要进行验证.从而实现方法加注解就需要验证是否登陆. 2.自定义 ...
- html button标签 语法
html button标签 语法 button标签怎么用? 作用:定义一个按钮. 语法:<button type="button">按钮</button> ...
- javaweb上传大文件的问题
总结一下大文件分片上传和断点续传的问题.因为文件过大(比如1G以上),必须要考虑上传过程网络中断的情况.http的网络请求中本身就已经具备了分片上传功能,当传输的文件比较大时,http协议自动会将文件 ...
- Tensorflow视频教程&Pytorch视频教程
基于tensorflow做研究和基于pytorch做研究哪个好?哪个更容易复制代码,工业上更易用.Keras和tensorflow.pytorch的关系. Keras:Keras是一个由Python编 ...
- wordcloud:让你的词语像云朵一样美
介绍 对文本中出现频率较高的关键词给予视觉化的显示 使用 python import jieba import codecs import wordcloud file = r"C:\U ...
- Python字典实现
这篇文章描述了在Python中字典是如何实现的. 字典通过键(key)来索引,它可以被看做是关联数组.我们在一个字典中添加3个键/值对: >>> d = {'a': 1, 'b': ...
- Python os模块方法
os模块提供了大量有用的方法来处理文件和目录.本章节中的代码实例是在 Ubuntu Linux系统上运行来演示. 大多数有用的方法都列在这里 - 编号 方法 描述/说明 1 os.access(pat ...
- leetcode-mid-math - 69. Sqrt(x)-NO
mycode memory error class Solution(object): def mySqrt(self, x): """ :type x: int : ...
- 【C++进阶】getline
在<istream>中的getline函数有两种重载形式: istream& getline (char* s, streamsize n );istream& getli ...
- 1、maven的下载,安装,配置
下载 1.maven官方下载地址: http://maven.apache.org/download.cgi 进入官网: 下载各历史版本官方地址: https://archive.apache.org ...