zoj 3195 Design the city lca倍增
给一棵树, m个询问, 每个询问给出3个点, 求这三个点之间的最短距离。
其实就是两两之间的最短距离加起来除2.
倍增的lca模板
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 1e5+;
int head[maxn*], num, dis[maxn], f[maxn], p[maxn][], deep[maxn], n;
struct node
{
int to, nextt, w;
}e[maxn*];
void add(int u, int v, int w) {
e[num].to = v, e[num].w = w, e[num].nextt = head[u], head[u] = num++;
}
void init() {
num = ;
mem1(head);
mem1(p);
}
void dfs(int u, int fa) {
f[u] = fa;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(v == fa)
continue;
dis[v] = dis[u]+e[i].w;
deep[v] = deep[u]+;
dfs(v, u);
}
}
void lca_init()
{
int i,j;
for(i = ; i<=n; i++)
p[i][] = f[i];
for(j = ; (<<j)<=n; j++) {
for(i = ; i<=n; i++) {
if(p[i][j-] != -)
p[i][j] = p[p[i][j-]][j-];
}
}
}
int lca(int a,int b)
{
int i,j;
if(deep[a]<deep[b])
swap(a, b);
for(i = ; (<<i)<=deep[a]; i++);
i--;
for(j = i; j>=; j--)
if(deep[a]-(<<j)>=deep[b])
a = p[a][j];
if(a == b)
return a;
for(j = i; j>=; j--)
{
if(p[a][j] != - && p[a][j] != p[b][j])
{
a = p[a][j];
b = p[b][j];
}
}
return f[a];
}
int main()
{
int x, y, m, w, z, ok = ;
while(cin>>n) {
if(ok)
puts("");
ok = ;
init();
for(int i = ; i<n-; i++) {
scanf("%d%d%d", &x, &y, &w);
add(x, y, w);
add(y, x, w);
}
dfs(, -);
lca_init();
cin>>m;
while(m--) {
scanf("%d%d%d", &x, &y, &z);
int ans = ;
ans += dis[x]+dis[y]-*dis[lca(x,y)];
ans += dis[x]+dis[z]-*dis[lca(x,z)];
ans += dis[y]+dis[z]-*dis[lca(y,z)];
printf("%d\n", ans/);
}
}
return ;
}
zoj 3195 Design the city lca倍增的更多相关文章
- ZOJ 3195 Design the city (LCA 模板题)
Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terribl ...
- zoj 3195 Design the city LCA Tarjan
题目链接 : ZOJ Problem Set - 3195 题目大意: 求三点之间的最短距离 思路: 有了两点之间的最短距离求法,不难得出: 对于三个点我们两两之间求最短距离 得到 d1 d2 d3 ...
- ZOJ 3195 Design the city LCA转RMQ
题意:给定n个点,下面n-1行 u , v ,dis 表示一条无向边和边权值,这里给了一颗无向树 下面m表示m个询问,问 u v n 三点最短距离 典型的LCA转RMQ #include<std ...
- zoj——3195 Design the city
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- ZOJ 3195 Design the city 题解
这个题目大意是: 有N个城市,编号为0~N-1,给定N-1条无向带权边,Q个询问,每个询问求三个城市连起来的最小权值. 多组数据 每组数据 1 < N < 50000 1 < Q ...
- ZOJ - 3195 Design the city
题目要对每次询问将一个树形图的三个点连接,输出最短距离. 利用tarjan离线算法,算出每次询问的任意两个点的最短公共祖先,并在dfs过程中求出离根的距离.把每次询问的三个点两两求出最短距离,这样最终 ...
- ZOJ Design the city LCA转RMQ
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- [zoj3195]Design the city(LCA)
解题关键:求树上三点间的最短距离. 解题关键:$ans = (dis(a,b) + dis(a,c) + dis(b,c))/2$ //#pragma comment(linker, "/S ...
- 算法笔记--lca倍增算法
算法笔记 模板: vector<int>g[N]; vector<int>edge[N]; ][N]; int deep[N]; int h[N]; void dfs(int ...
随机推荐
- redis存储session配制方法
redis存储session配制方法需要三个模块: 1.redis 2.express-session 3.connect-redis 项目中的配置方法代码片段如下: 首先连接redis,连接redi ...
- [置顶] ios App 中嵌入应用商店
昨晚同事拿了一个app 发现其app 内部页面打开了appstore 并没有唤起手机自带的appstore, 刚开始以为是用webview 加载的 ,可是自己些了一个demo 发现并不是那样一回事 用 ...
- MongoDB 启动异常
今天启动MongoDB遇到异常状况 mongodb warning: 32-bit servers don't have journaling enable 解决方法: 删除数据库目录的.lock文件 ...
- java 对象数组定义
下面代码实现了定义一个数组对象 public class Student { private String username; private int num; public Student(Stri ...
- Deep Learning(深度学习)学习笔记整理系列之(三)
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
- 通过git和Xcode将代码上传到GitHub
长话短说: 第一步:初始化一个本地仓库 git init 第二步:将你的文件添加到缓存区 git add . 后面的空格 . 是将全部的文件都添加到缓存区 可以使用git status 查看状态 ...
- .Net平台-MVP模式初探(一)
为什么要写这篇文章 笔者当前正在负责研究所中一个项目,这个项目基于.NET平台,初步拟采用C/S部署体系,所以选择了Windows Forms作为其UI.经过几此迭代,我们发现了一个问题:虽然业务逻辑 ...
- Knuth-Morris-Pratt Algorithm
Today , 第一次学习KMP Algorithm,其中好多地方还是不能理解的透彻,本文将进一步对 KMP Algorithm 进行学习,搞清楚其中的思想…… First , KMP Algorit ...
- Models——英语学习小技巧之四
Models are very important, here model means role model, is kind of like a hero. It's someone that ...
- MyEclipseアンロックの手順
↓ ↓ ↓ ↓ ↓ ↓