给一棵树, 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-8; const int mod = 1e9+7; const int inf = 1061109567; const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; const int maxn = 1e5+5; int head[maxn*2], num, dis[maxn], f[maxn], p[maxn][20], deep[maxn], n; struct node { int to, nextt, w; }e[maxn*2]; 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 = 0; 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]+1; dfs(v, u); } } void lca_init() { int i,j; for(i = 1; i<=n; i++) p[i][0] = f[i]; for(j = 1; (1<<j)<=n; j++) { for(i = 1; i<=n; i++) { if(p[i][j-1] != -1) p[i][j] = p[p[i][j-1]][j-1]; } } } int lca(int a,int b) { int i,j; if(deep[a]<deep[b]) swap(a, b); for(i = 0; (1<<i)<=deep[a]; i++); i--; for(j = i; j>=0; j--) if(deep[a]-(1<<j)>=deep[b]) a = p[a][j]; if(a == b) return a; for(j = i; j>=0; j--) { if(p[a][j] != -1 && 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 = 0; while(cin>>n) { if(ok) puts(""); ok = 1; init(); for(int i = 0; i<n-1; i++) { scanf("%d%d%d", &x, &y, &w); add(x, y, w); add(y, x, w); } dfs(0, -1); lca_init(); cin>>m; while(m--) { scanf("%d%d%d", &x, &y, &z); int ans = 0; ans += dis[x]+dis[y]-2*dis[lca(x,y)]; ans += dis[x]+dis[z]-2*dis[lca(x,z)]; ans += dis[y]+dis[z]-2*dis[lca(y,z)]; printf("%d\n", ans/2); } } return 0; }
时间: 2024-12-09 19:23:22