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 ...
随机推荐
- Android 多屏幕适配 dp和px的关系
一直以来别人经常问我,android的多屏幕适配到底是怎么弄,我也不知道如何讲解清楚,或许自己也是挺迷糊. 以下得出的结论主要是结合官方文档进行分析的https://developer.android ...
- 【Python】Linux crontab定时任务配置方法(详解)
CRONTAB概念/介绍 crontab命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行. cron 系统调度进程. 可以使用它在 ...
- hibernate笔记(二)
目标: 关联映射(hibernate映射) 1. 集合映射 2. 一对多与多对一映射 (重点) 3. 多对多映射 4. inverse/lazy/cascade 1. 集合映射 开发流程: 需求分析/ ...
- poj 2155 Matrix (树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16797 Accepted: 6312 Descripti ...
- OJ 列表
维护一个 OJ 列表.(这件事似乎没啥意义?) AtCoder hihoCoder Codeforces DMOJ CodeChef CS Academy HackerRank HackerEarth ...
- [BZOJ3473][BZOJ3277]字符串
[BZOJ3473][BZOJ3277]字符串 试题描述 给定 \(n\) 个字符串,询问每个字符串有多少子串(不包括空串)是所有 \(n\) 个字符串中至少 \(k\) 个字符串的子串? 输入 第一 ...
- [NOIP2018 TG D2T1]旅行
题目大意:$NOIP\;TG\;D2T1$ 题解:一棵树的很简单,第一个点一定是$1$,只需要对每个节点,找最小的没有访问过的节点访问即可,我写的是$O(n\log_2n)$. 考虑基环树的部分,一个 ...
- Spring源码解析-事件
Spring事件的组件 主要是3个组件: 1.ApplicationEvent 事件 2.ApplicationListener 监听器,对事件进行监听 3.ApplicationEventMul ...
- ng websocket
ng使用websocket 1.安装依赖库npm install ws --save 2.安装类型定义文件 npm install @types/ws --save 3.编写服务 import { I ...
- ViewData和ViewBag的那些事
既然结论是“共享着相同的数据”,那我们就证实一下吧. 看来结论是正确的. 去查看定义,发现他们的类型是不一样的,ViewData是ViewDataDictionary,ViewBag是dynamic. ...