How far away ? LCA求树上两点距离
How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 22976 Accepted Submission(s): 9078
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 <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("DATA.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 1e5 + ;
int _pow[maxn], dep[maxn], dis[maxn], vis[maxn], ver[maxn];
int tot, head[maxn], dp[maxn * ][], k, first[maxn];
struct node {
int u, v, w, nxt;
} edge[maxn << ];
void init() {
tot = ;
mem(head, -);
}
void add(int u, int v, int w) {
edge[tot].v = v, edge[tot].u = u;
edge[tot].w = w, edge[tot].nxt = head[u];
head[u] = tot++;
}
void dfs(int u, int DEP) {
vis[u] = ;
ver[++k] = u;
first[u] = k;
dep[k] = DEP;
for (int i = head[u]; ~i; i = edge[i].nxt) {
if (vis[edge[i].v]) continue;
int v = edge[i].v, w = edge[i].w;
dis[v] = dis[u] + w;
dfs(v, DEP + );
ver[++k] = u;
dep[k] = DEP;
}
}
void ST(int len) {
int K = (int)(log((double)len) / log(2.0));
for (int i = ; i <= len ; i++) dp[i][] = i;
for (int j = ; j <= K ; j++) {
for (int i = ; i + _pow[j] - <= len ; i++) {
int a = dp[i][j - ], b = dp[i + _pow[j - ]][j - ];
if (dep[a] < dep[b]) dp[i][j] = a;
else dp[i][j] = b;
}
}
}
int RMQ(int x, int y) {
int K = (int)(log((double)(y - x + )) / log(2.0));
int a = dp[x][K], b = dp[y - _pow[K] + ][K];
if (dep[a] < dep[b]) return a;
else return b;
}
int LCA(int u, int v) {
int x = first[u], y = first[v];
if (x > y) swap(x, y);
int ret = RMQ(x, y);
return ver[ret];
}
int main() {
for (int i = ; i < ; i++) _pow[i] = ( << i);
int t;
sf(t);
while(t--) {
int n, q;
init();
sff(n, q);
mem(vis, );
for (int i = ; i < n ; i++) {
int u, v, w;
sfff(u, v, w);
add(u, v, w);
add(v, u, w);
}
k = , dis[] = ;
dfs(, );
ST( * n - );
while(q--) {
int u, v;
sff(u, v);
int lca = LCA(u, v);
printf("%d\n", dis[u] + dis[v] - * dis[lca]);
}
}
return ;
}
How far away ? LCA求树上两点距离的更多相关文章
- 模板倍增LCA 求树上两点距离 hdu2586
http://acm.hdu.edu.cn/showproblem.php?pid=2586 课上给的ppt里的模板是错的,wa了一下午orz.最近总是被坑啊... 题解:树上两点距离转化为到根的距离 ...
- Codeforces Round #620 (Div. 2)E(LCA求树上两点最短距离)
LCA求树上两点最短距离,如果a,b之间距离小于等于k并且奇偶性与k相同显然YES:或者可以从a先走到x再走到y再走到b,并且a,x之间距离加b,y之间距离+1小于等于k并且奇偶性与k相同也输出YES ...
- cogs 2450. 距离 树链剖分求LCA最近公共祖先 快速求树上两点距离 详细讲解 带注释!
2450. 距离 ★★ 输入文件:distance.in 输出文件:distance.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 在一个村子里有N个房子,一 ...
- hdu6446 网络赛 Tree and Permutation(树形dp求任意两点距离之和)题解
题意:有一棵n个点的树,点之间用无向边相连.现把这棵树对应一个序列,这个序列任意两点的距离为这两点在树上的距离,显然,这样的序列有n!个,加入这是第i个序列,那么这个序列所提供的贡献值为:第一个点到其 ...
- LCA - 求任意两点间的距离
There are n houses in the village and some bidirectional roads connecting them. Every day peole alwa ...
- HDU 2586 /// tarjan离线求树上两点的LCA
题目大意: 询问一棵树里 u 到 v 的距离 可由 dis[ u到根 ] + dis[ v到根 ] - 2*dis[ lca(u,v) ] 得到 https://blog.csdn.net/csyzc ...
- Codeforces 1304E. 1-Trees and Queries 代码(LCA 树上两点距离判奇偶)
https://codeforces.com/contest/1304/problem/E #include<bits/stdc++.h> using namespace std; typ ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】
Tree and Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- POJ 4718 /// 树链剖分+线段树区间合并 求树上两点间的LCIS长度
题目大意: 给定n个点 每个点都有权值 接下来给定树的n条边 第 i 个数 a[i] 表示 i+1到a[i]之间 有一条边 给定q q个询问 每次询问给出 x y 求x到y的最长上升子序列的长度 题解 ...
随机推荐
- SqlServer的两种插入方式效率对比
protected void button1_Click(object sender, EventArgs e) { DataTable dtSource = new DataTable(); dtS ...
- 配置vConsole调试console
1.使用 npm 安装: npm install vconsole 再使用webpack,然后js代码中 import VConsole from 'vconsole/dist/vconsole.mi ...
- 百度翻译api 实现简易微信翻译小程序
介绍 口袋翻译 口袋翻译 微信小程序 翻译功能 含7类语言的相互翻译 包含最近10条的翻译历史回溯功能 微信搜索:简e翻译 功能展示 使用百度翻译api需要申请 appid 与 key 并在 ap ...
- ES6 之 let / const
本博文配合 阮一峰 <ES6 标准入门(第3版)>一书进行简要概述 ES6 中的 let 与 const. 历史遗留问题 由于 JS ES3语法中的 var 提升变量.没有块级作用域,因而 ...
- [转载]CENTOS 6.0 iptables 开放端口80 3306 22端口
原文地址:6.0 iptables 开放端口80 3306 22端口">CENTOS 6.0 iptables 开放端口80 3306 22端口作者:云淡风轻 #/sbin/iptab ...
- 【MFC】学习与问题整合
需要源码联系邮件:kangxlchn@163.com 1.新建一个MFC工程(基于对话框) 环境:vs2017 统统NEXT 新建完成后打开MFCPrj.cpp文件 打开类试图 每创建一个MFC项目, ...
- Train Problem(栈的应用)
Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of studen ...
- RabbitMQ基本模式
最近用到了一些RabbitMQ的东西,看了官方的Get Started,以此为模板总结一下. (1)生产者(发送方)发送消息到ExChange(含参:routingkey),ExChange通过bin ...
- win7主题/默认账户图片路径
账户图片位置 C:\ProgramData\Microsoft\User Account Pictures 主题位置 C:\Windows\Resources
- SQL SERVER技术内幕之10 可编程对象
一.变量 变量用于临时保存数据值,以供在声明它们的同一批处理语句中引用.例如,以下代码先声明一个数据类型为INT的变量@i,再将它赋值为10; DECLARE @i as INT; SET @i = ...