题目链接

给一棵树, 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倍增的更多相关文章

  1. 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 ...

  2. zoj 3195 Design the city LCA Tarjan

    题目链接 : ZOJ Problem Set - 3195 题目大意: 求三点之间的最短距离 思路: 有了两点之间的最短距离求法,不难得出: 对于三个点我们两两之间求最短距离 得到 d1 d2 d3 ...

  3. ZOJ 3195 Design the city LCA转RMQ

    题意:给定n个点,下面n-1行 u , v ,dis 表示一条无向边和边权值,这里给了一颗无向树 下面m表示m个询问,问 u v n 三点最短距离 典型的LCA转RMQ #include<std ...

  4. zoj——3195 Design the city

    Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...

  5. ZOJ 3195 Design the city 题解

    这个题目大意是: 有N个城市,编号为0~N-1,给定N-1条无向带权边,Q个询问,每个询问求三个城市连起来的最小权值. 多组数据 每组数据  1 < N < 50000  1 < Q ...

  6. ZOJ - 3195 Design the city

    题目要对每次询问将一个树形图的三个点连接,输出最短距离. 利用tarjan离线算法,算出每次询问的任意两个点的最短公共祖先,并在dfs过程中求出离根的距离.把每次询问的三个点两两求出最短距离,这样最终 ...

  7. ZOJ Design the city LCA转RMQ

    Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...

  8. [zoj3195]Design the city(LCA)

    解题关键:求树上三点间的最短距离. 解题关键:$ans = (dis(a,b) + dis(a,c) + dis(b,c))/2$ //#pragma comment(linker, "/S ...

  9. 算法笔记--lca倍增算法

    算法笔记 模板: vector<int>g[N]; vector<int>edge[N]; ][N]; int deep[N]; int h[N]; void dfs(int ...

随机推荐

  1. ubuntu13.04安装SenchaArchitect-2.2无法启动的问题

    系统是ubuntukylin-13.04-desktop版本,不知道别的版本有没有这个问题,未测试.SenchaArchitect采用最新版本2.2.2,我安装的是32位的. 具体无法启动的问题如下: ...

  2. UVAlive 2911 Maximum(贪心)

    Let x1, x2,..., xm be real numbers satisfying the following conditions: a) -xi ; b) x1 + x2 +...+ xm ...

  3. oracle tns

    oracle tns 是oracle提供的服务名,设置方法,oracle安装根目录---product----版本选择11.2.0----client1---NETWORK---ADMIN---tns ...

  4. VBA 简单调试

    在中断模式下(ctrl+Break键),可以做: 1.执行    工具----选项----编辑器----勾选“自动显示数据提示” 则当用鼠标悬停在变量或表达式上时,会出现提示窗口,显示其名称和值! 2 ...

  5. 一、富有表现力的JavaScript

    第一章:富有表现力的JavaScript 1.1  JavaScript的灵活性 1.2  弱类型语言 1.3  函数是一等对象 1.4  对象的易变性 1.5  继承 1.6  JavaScript ...

  6. [翻译]如何用YII写出安全的WEB应用

    前言 虽然本文是基于YII1.1,但其中提到的安全措施适用于多数web项目安全场景,所以翻译此文,跟大家交流.原文地址. 目录 安全基本措施... 2 验证与过滤用户的输入信息... 2 原理... ...

  7. adb wifi连接手机

    1. 默认情况下,ADB是通过USB来进行连接的. 不需要USB线,直接在android设备上安装一个超级终端,在终端里运行以下代码即可: su setprop service.adb.tcp.por ...

  8. [转载]标签a的href和onclick

    转载自:http://gocom.primeton.com/blog21307_27051.htm 我以前在写<A>的href和onclick一直很随意,后来出过几次问题,以后才开始重视这 ...

  9. 虚拟机NAT模式主机ping不通虚拟机解决方案

    本篇没有抓包,只是简单一个实施.需要的童鞋可以拿走这个方法. 虚拟机与真机通信三种模式, 桥接模式,NAT 模式 ,HOST 模式. 桥接就是在真机的网络上模拟一个网卡,给虚拟机申请一个和真机在同一个 ...

  10. 每天一个小算法(5)----找到链表倒数第K个结点

    估计这个问题在面试中被问烂了. 思路是先找到正数的第K个结点的指针pT,然后和指向头结点的指针pN一起向后移动,直到第K个指针指向NULL,此时pN指向的结点即倒数第K个结点. 如图: #includ ...