zoj 3195 Design the city lca倍增

时间:2024-10-13 20:33:50

题目链接

给一棵树, 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 ;
}