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 ...
随机推荐
-
typeof,GetType
typeof: 是运算符,获得某一类型的 System.Type 对象. Int32 t = new Int32(); Type t = typeof(int); GetType: 是方法,获取当前实 ...
-
用java实现冒泡排序法
一.基本思路: 冒泡排序是一种简单的交换类排序.其基本思路是,从头开始扫描待排序的元素,在扫描过程中依次对相邻元素进行比较,将关键字值大的元素后移.每经过一趟排序后,关键字值最大的元素将移到末尾,此时 ...
-
Aborting a running program
In the event that a calculation appears to be running excessively long, one can abort thecalculation ...
-
在Weex中定制自定义组件
1.配置自定义组件 public class MyViewComponent extends WXComponent{ public MyViewComponent(WXSDKInstance ins ...
-
Git 企业开发者教程
为什么要写这样一个面向企业开发者的Git教程?这个问题也困扰我自己很久.其实我使用git的时间也不短了,但是就和正在阅读本文的每一位一样,常用的基本就是那么几个(git clone, git pu ...
-
qsv文件转码mp4格式过程记录
之前帮一个朋友剪辑配音视频,源文件在爱奇艺里,特有的qsv格式让我白忙活一下午. 晚上趁着有空,在网上查找资料,翻阅了很多文件,都让我无从下手. 基本都是一个套路,转成fiv格式,再转mp4格式,但是 ...
-
MySQL SET数据类型
SET: 多选字符串数据类型,适合存储“多个值”. 设定set的时候,同样需要设定“固定的几个值”:存储的时候,可以存储其中的若干个值. 设定set的格式: 字段名称 SET("选项1&q ...
-
树&#183;AVL树/平衡二叉树
1.AVL树 带有平衡条件的二叉查找树,所以它必须满足条件: 1 是一棵二叉查找树 2 满足平衡条件 1.1 平衡条件: 1)严格的平衡条件:每个节点都必须有相同高度的左子树和右子树(过于严格而不被使 ...
-
19. Rootkit detectors (隐形工具包检测器 5个)
Sysinternals提供了许多小型Windows实用程序,对于低级别的Windows黑客攻击来说非常有用. 一些是免费的和/或包括源代码,而其他是专有的. 调查受访者最喜欢:ProcessExpl ...
-
Technical
CAN FD (CAN with Flexible Data-Rate) is an extension to the original CAN bus protocol specified in I ...