HDU 2586.How far away ?-离线LCA(Tarjan)
2586.How far away ?
这个题以前写过在线LCA(ST)的,HDU2586.How far away ?-在线LCA(ST)
现在贴一个离线Tarjan版的
代码:
//A-HDU2586-LCA-tarjan离线版
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=4e4+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); struct node{
int x,y;
//node(int xx,int yy){
// x=xx,y=yy;
//}node(){}
}; vector<node> edge[maxn],q[maxn];
int ans[maxn],dis[maxn],fa[maxn],vis[maxn];
int n,m,aa,bb,cc; int find(int x)//找父节点(祖先)
{
return x==fa[x]?x:fa[x]=find(fa[x]);
} void unio(int x,int y)//并查集压缩路径
{
fa[find(y)]=find(x);
} void init()//初始化
{
for(int i=;i<=n;i++){
edge[i].clear();
q[i].clear();
fa[i]=i;//初始化自己的父亲节点是自己
vis[i]=;
ans[i]=;
dis[i]=;
}
} void dfs(int x)//tarjan
{
vis[x]=;
for(int i=;i<edge[x].size();i++){
int v=edge[x][i].x;
if(!vis[v]){
dis[v]=dis[x]+edge[x][i].y;
dfs(v);
unio(x,v);
}
}
for(int i=;i<q[x].size();i++){
int v=q[x][i].x;
if(vis[v])
ans[q[x][i].y]=dis[x]+dis[v]-*dis[find(v)];
}
} int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
init();
for(int i=;i<n;i++){
scanf("%d%d%d",&aa,&bb,&cc);
//edge[aa].push_back(node(bb,cc));
//edge[bb].push_back(node(aa,cc));
edge[aa].push_back({bb,cc});
edge[bb].push_back({aa,cc});
}
for(int i=;i<=m;i++){
scanf("%d%d",&aa,&bb);
//q[aa].push_back(node(bb,i));
//q[bb].push_back(node(aa,i));
q[aa].push_back({bb,i});
q[bb].push_back({aa,i}); }
dfs();
for(int i=;i<=m;i++)
printf("%d\n",ans[i]);
}
}
溜了。
HDU 2586.How far away ?-离线LCA(Tarjan)的更多相关文章
- 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 ...
- 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算法)
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 ...
随机推荐
- [剑指Offer] 10.矩形覆盖
题目描述 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? [思路]可归纳得出结论: f(n) = f(n-1) + f ...
- hdu 1207 汉诺塔II (DP+递推)
汉诺塔II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- poj 1034 The dog task (二分匹配)
The dog task Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2559 Accepted: 1038 Sp ...
- [BZOJ3594] [Scoi2014]方伯伯的玉米田 二维树状数组优化dp
我们发现任何最优解都可以是所有拔高的右端点是n,然后如果我们确定了一段序列前缀的结尾和在此之前用过的拔高我们就可以直接取最大值了然后我们在这上面转移就可以了,然后最优解用二维树状数组维护就行了 #in ...
- 安卓下拉刷新空间SwipeRefreshLayout的基本使用
1.先写布局文件 <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/refresh" androi ...
- codeforces 1077D
题目:https://codeforces.com/contest/1077/problem/D 题意:给你一个长度为n的串,你需要在里面找到出现次数最多的长度为k的子序列(子序列中元素可重复),求这 ...
- eclipse关闭错误警告提示
- linux基础(2)
Linux基础题 作业一:1) 新建用户natasha,uid为1000,gid为555,备注信息为“master”useradd natashagroupmod -g 555 natashauser ...
- bzoj1503 郁闷的出纳员 splay版
自己yy的写法 可能有点奇怪吧 详情看代码 还是蛮短的 #include<cstdio> #include<cstring> #include<algorithm> ...
- codeforces613B - Skills &&金中市队儿童节常数赛
题目传送门 本随笔写的是第二题...... 这道题方法就是搞乱....因为n较mxa小 所以枚举达到最大上限的点 然后就乱搞 代码看看咯 #include<cstdio> #include ...