caioj 1237: 【最近公共祖先】树上任意两点的距离 在线倍增ST
caioj 1237: 【最近公共祖先】树上任意两点的距离 倍增ST
题目链接:http://caioj.cn/problem.php?id=1237
思路:
- 针对询问次数多的时候,采取倍增求取LCA,同时跟新距离数组
- 因为
- \(2^{14} > 10000\)
- 所以所以表示祖先的数组dp[][]第二维取到14即可
代码:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
const int maxn = 10005;
const int maxm = 20005;
struct node {
int to,next,w;
}edges[maxm];
int head[maxn],cnt,dp[maxn][15],dep[maxn],dist[maxn];
void addedge(int u, int v, int w) {
edges[cnt].to=v;
edges[cnt].w=w;
edges[cnt].next=head[u];
head[u]=cnt++;
}
void dfs(int s, int x) {
dep[s]=dep[x]+1;
dp[s][0]=x;
int t;
for(int i=1;(1<<i)<=dep[s];++i)
dp[s][i]=dp[dp[s][i-1]][i-1];
for(int i=head[s];i!=-1;i=edges[i].next) {
t=edges[i].to;
if(t==x) continue;
dist[t]=dist[s]+edges[i].w;
dfs(t,s);
}
}
int lca(int u, int v) {
if(dep[v]>dep[u]) swap(u,v);
for(int i=14;i>=0;--i) {
if((1<<i)<=(dep[u]-dep[v])) {
u=dp[u][i];
}
}
if(u==v) return u;
for(int i=14;i>=0;--i) {
if((1<<i)<=dep[u]&&(dp[u][i]!=dp[v][i])) {
u=dp[u][i];
v=dp[v][i];
}
}
return dp[u][0];
}
int slove(int u ,int v) {
int z=lca(u,v);
return dist[u]-2*dist[z]+dist[v];
}
void init() {
cnt=0;
memset(head,-1,sizeof(head));
}
int main() {
int n,m,u,v,w;
scanf("%d %d",&n,&m);
init();
for(int i=1;i<n;++i) {
scanf("%d %d %d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
dep[1]=0;//为了统一dfs的写法,实际dep[1]=1
dp[1][0]=1;
dfs(1,1);
for(int i=1;i<=m;++i) {
scanf("%d %d",&u,&v);
printf("%d\n",slove(u,v));
}
return 0;
}
caioj 1237: 【最近公共祖先】树上任意两点的距离 在线倍增ST的更多相关文章
- HDU 2376 树形dp|树上任意两点距离和的平均值
原题:http://acm.hdu.edu.cn/showproblem.php?pid=2376 经典问题,求的是树上任意两点和的平均值. 这里我们不能枚举点,这样n^2的复杂度.我们可以枚举每一条 ...
- HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)
Problem DescriptionAn abandoned country has n(n≤100000) villages which are numbered from 1 to n. Sin ...
- JAVA 计算地球上任意两点(经纬度)距离
/** * 计算地球上任意两点(经纬度)距离 * * @param long1 * 第一点经度 * @param lat1 * 第一点纬度 * @param long2 * 第二点经度 * @para ...
- caioj 1236 最近公共祖先 树倍增算法模版 倍增
[题目链接:http://caioj.cn/problem.php?id=1236][40eebe4d] 代码:(时间复杂度:nlogn) #include <iostream> #inc ...
- [luogu3379]最近公共祖先(树上倍增求LCA)
题意:求最近公共祖先. 解题关键:三种方法,1.st表 2.倍增法 3.tarjan 此次使用倍增模板(最好采用第一种,第二种纯粹是习惯) #include<cstdio> #includ ...
- 洛谷P3379 【模板】最近公共祖先(LCA)(dfs序+倍增)
P3379 [模板]最近公共祖先(LCA) 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询 ...
- git是一种分布式代码管理工具,git通过树的形式记录文件的更改历史,比如: base'<--base<--A<--A' ^ | --- B<--B' 小米工程师常常需要寻找两个分支最近的分割点,即base.假设git 树是多叉树,请实现一个算法,计算git树上任意两点的最近分割点。 (假设git树节点数为n,用邻接矩阵的形式表示git树:字符串数组matrix包含n个字符串,每个字符串由字符'0
// ConsoleApplication10.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...
- HDU2376Average distance(树形dp|树上任意两点距离和的平均值)
思路: 引:如果暴力枚举两点再求距离是显然会超时的.转换一下思路,我们可以对每条边,求所有可能的路径经过此边的次数:设这条边两端的点数分别为A和B,那 么这条边被经过的次数就是A*B,它对总的距离和的 ...
- 【非原创】codeforces 1060E Sergey and Subway 【树上任意两点距离和】
学习博客:戳这里 本人代码: 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 con ...
随机推荐
- MongoDB聚合(count、distinct、group、MapReduce)
1. count:返回集合中文档的数量. db.friend.count() db.friend.count({'age':24}) 增加查询条件会使count查询变慢. 2. distinct:找出 ...
- VS2012 C#生成DLL并调用
1.创建一个C#工程生成DLL 新建->项目->Visual C#->类库->MyMethods 项目建好后,为了理解,将项目中的Class1.cs 文件 重命名为 MySwa ...
- POJ1222EXTENDED LIGHTS OUT(高斯消元)
EXTENDED LIGHTS OUT Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11815 Accepted: 7 ...
- Problem F
Problem Description "Yakexi, this is the best age!" Dong MW works hard and get high pay, h ...
- Line belt
Problem Description In a two-dimensional plane there are two line belts, there are two segments AB a ...
- SVN提交文件的时候过滤指定文件
如果使用TortoiseSVN作为客户端的话,可以在TortoiseSVN右键菜单中的 "设置"(settings)--常规设置(General)--全局忽略样式里设置(Globa ...
- 大话git中的撤销操作
下面以现实场景作为情境. 基础知识,理解git中的几个区域 本地代码已经add,未commit 修改本地工作目录中的readme.md,添加文字"第一次修改" 然后查看下状态 ➜ ...
- Scrum Meeting Alpha - 3
Scrum Meeting Alpha - 3 NewTeam 2017/10/27 地点:新主楼F座二楼 任务反馈 团队成员 完成任务 计划任务 安万贺 找到了几个开源项目,参考了API的包装方式, ...
- python迭代器以及itertools模块
迭代器 在python中,迭代器协议就是实现对象的__iter()方法和next()方法,其中前者返回对象本身,后者返回容器的下一个元素.实现了这两个方法的对象就是可迭代对象.迭代器是有惰性的,只有在 ...
- 使用python3的typing模块提高代码健壮性
前言:很多人在写完代码一段时间后回过头看代码,很可能忘记了自己写的函数需要传什么参数,返回什么类型的结果,就不得不去阅读代码的具体内容,降低了阅读的速度,加上Python本身就是一门弱类型的语言,这种 ...