HDU 2586 How far away ? 离线lca模板题
How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8712 Accepted Submission(s): 3047
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
题意
给你一棵树,每次询问两点间的距离。
题解
跑一发离线lca,每次询问u,v,设c是u,v的lca,那么答案就是dis[u]+dis[v]-2*dis[c],其中dis表示从根到节点的距离。详见代码:
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#define MAX_N 40003
using namespace std; bool vis[MAX_N]; struct edge {
public:
int to;
long long cost; edge(int t, long long c) : to(t), cost(c) { } edge() { }
}; vector<edge> G[MAX_N];
int q,n,m; struct node {
public:
int p, v; node(int pp, int vv) : p(pp), v(vv) { } node() { }
}; vector<node> Q[MAX_N];
int lca[MAX_N], ancestor[MAX_N]; int T, cas = ; int father[MAX_N];
void init() {
for (int i = ; i <= n; i++)
father[i] = i;
} int Find(int u) {
if (u == father[u])return u;
else return father[u] = Find(father[u]);
} void unionSet(int u,int v) {
int x = Find(u), y = Find(v);
if (x == y)return;
father[x] = y;
} bool Same(int u,int v) {
return Find(u) == Find(v);
} long long dis[MAX_N]; void Tarjan(int u,int p) {
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i].to;
if (v == p)continue;
dis[v] = dis[u] + G[u][i].cost;
Tarjan(v, u);
unionSet(u, v);
ancestor[Find(u)] = u;
}
vis[u] = ;
for (int i = ; i < Q[u].size(); i++) {
int v = Q[u][i].v;
if (vis[v])
lca[Q[u][i].p] = ancestor[Find(v)];
}
} pair<int, int> qu[MAX_N]; int main() {
//cin.sync_with_stdio(false);
scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &q);
m = n - ;
init();
memset(vis, , sizeof(vis));
memset(ancestor, , sizeof(ancestor));
memset(dis, , sizeof(dis));
memset(lca, , sizeof(lca));
for (int i = ; i <= n; i++)G[i].clear();
for (int i = ; i <= n; i++)Q[i].clear(); for (int i = ; i < m; i++) {
int u, v;
long long c;
scanf("%d%d%I64d", &u, &v, &c);
G[u].push_back(edge(v, c));
G[v].push_back(edge(u, c));
} for (int i = ; i < q; i++) {
int u, v;
scanf("%d%d", &u, &v);
qu[i] = make_pair(u, v);
Q[u].push_back(node(i, v));
Q[v].push_back(node(i, u));
}
Tarjan(, ); for (int i = ; i < q; i++)
printf("%I64d\n", dis[qu[i].first] + dis[qu[i].second] - * dis[lca[i]]);
}
return ;
}
HDU 2586 How far away ? 离线lca模板题的更多相关文章
- 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模板)
题意: N个点,形成一棵树,边有长度. M个询问,每个询问(a,b),询问a和b的距离 思路: 模板题,看代码.DFS预处理算出每个结点离根结点的距离. 注意: qhead[maxn],而不是qhea ...
- 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模板题)
HDU - 2586 How far away ? Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & ...
- HDU 2586——How far away ?——————【LCA模板题】
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 2586 How far away ? (LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 LCA模版题. RMQ+LCA: #include <iostream> #incl ...
- HDU 4280:Island Transport(ISAP模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...
- HDU 4347 - The Closest M Points - [KDTree模板题]
本文参考: https://www.cnblogs.com/GerynOhenz/p/8727415.html kuangbin的ACM模板(新) 题目链接:http://acm.hdu.edu.cn ...
- HDU 2089 不要62(数位dp模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求区间内不包含4和连续62的数的个数. 思路: 简单的数位dp模板题.给大家推荐一个好的讲解博客.h ...
随机推荐
- java代码导出数据到Excel、js导出数据到Excel(三)
jsp内容忽略,仅写个出发按钮: <button style="width: 100px" onclick="expertExcel()&quo ...
- HMAC(Hash-based Message Authentication Code)实现原理
1.HMAC 概念 HMAC(Hash-based Message Authentication Code)基于 hash 的消息验证码,是 安全通信中必要的组成部件. 主要是 防止消息被篡改,和对称 ...
- 求数组中两两相加等于20的组合(Python实现)
题目 求数组中两两相加等于20的组合. 例:给定一个数组[1, 7, 17, 2, 6, 3, 14],这个数组中满足条件的有两对:17+3=20, 6+14=20. 解析 分为两个步骤: 先采用堆排 ...
- P3369 【模板】普通平衡树 Treap
P3369 [模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询 ...
- dubbo doc入门文档
dubbo document http://dubbo.apache.org/zh-cn/docs/user/references/xml/dubbo-protocol.html
- 光学字符识别OCR-7语言模型
由于图像质量等原因,性能再好的识别模型,都会有识别错误的可能性,为了减少识别错误率,可以将识别问题跟统计语言模型结合起来,通过动态规划的方法给出最优的识别结果.这是改进OCR识别效果的重要方法之一. ...
- Leetcode28--->字符串的匹配(KMP)
题目: 题目的本质是给定两个字符串str1,str2,求str1中的str2串开始的地方,即字符串的匹配,KMP算法 思路:时间复杂度为O(m + n),空间复杂度为O(n),原串的长度为m,子串的长 ...
- 服务器迁移至Linux操作系统
我在这里试了ubuntu.Debian,centos.最终还是选择了centos 使用工具putty,远程桌面的话使用vnc viewer(看起来service文件更改只需要替换user,但是路径不对 ...
- solr 创建core
mkdir /var/solr/data/CORE_NAME cp -r /opt/solr/server/solr/configsets/basic_configs/* /var/solr/data ...
- Tomcat自动发布war包
有两种方法: 1.将项目打成war包,复制到${tomcat.home}\webapps目录下.当tomcat启动时会自动将其解包. 有人说,不能直接将war文件夹直接复制到${tomcat.home ...