当前位置:网站首页>HDU 2586 How far away ? (LCA multiplication method)
HDU 2586 How far away ? (LCA multiplication method)
2022-07-28 04:55:00 【gongyuandaye】
The question : Give a spanning tree with edge weight ,q Time to ask , seek u、v The length of the path between .
Answer key :lca
Multiplication method .
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
#define pii pair<int, int>
using namespace std;
const int MAXN = 4e4 + 5;
int t, n, q;
vector<int> v[MAXN], w[MAXN];
int fa[MAXN][31], cost[MAXN][31], dep[MAXN];
void init() {
for (int i = 1; i <= n; i++) v[i].clear(), w[i].clear();
/*memset(fa, 0, sizeof(fa)); memset(cost, 0, sizeof(cost)); memset(dep, 0, sizeof(dep));*/
}
void dfs(int root, int fno) {
//1 0
fa[root][0] = fno;
dep[root] = dep[fa[root][0]] + 1;
for (int i = 1; i < 31; ++i) {
fa[root][i] = fa[fa[root][i - 1]][i - 1];
cost[root][i] = cost[fa[root][i - 1]][i - 1] + cost[root][i - 1];
}
int sz = v[root].size();
for (int i = 0; i < sz; ++i) {
if (v[root][i] == fno) continue;
cost[v[root][i]][0] = w[root][i];
dfs(v[root][i], root);
}
}
int lca(int x, int y) {
if (dep[x] > dep[y]) swap(x, y);
int tmp = dep[y] - dep[x], ans = 0;
for (int j = 0; tmp; ++j, tmp >>= 1)
if (tmp & 1) ans += cost[y][j], y = fa[y][j];
if (y == x) return ans;
for (int j = 30; j >= 0 && y != x; --j) {
if (fa[x][j] != fa[y][j]) {
ans += cost[x][j] + cost[y][j];
x = fa[x][j];
y = fa[y][j];
}
}
ans += cost[x][0] + cost[y][0];
return ans;
}
int main() {
scanf("%d", &t);
int x, y, k;
while (t--) {
init();
scanf("%d%d", &n, &q);
for (int i = 1; i < n; i++) {
scanf("%d%d%d", &x, &y, &k);
v[x].push_back(y);
v[y].push_back(x);
w[x].push_back(k);
w[y].push_back(k);
}
dfs(1, 0);
while (q--) {
scanf("%d%d", &x, &y);
printf("%d\n", lca(x, y));
}
}
return 0;
}
边栏推荐
- Histogram of pyplot module of Matplotlib (hist(): basic parameter, return value)
- The difference between alter and confirm, prompt
- Comprehensively analyze the differences between steam and maker Education
- Redux basic syntax
- [Sylar] framework -chapter9-hook module
- Improve the core quality of steam education among students
- Summary and review of puppeter
- printf()打印char* str
- [Oracle] 083 wrong question set
- Euler road / Euler circuit
猜你喜欢

How to quickly locate bugs? How to write test cases?

01 node express system framework construction (express generator)

After easycvr is connected to the national standard equipment, how to solve the problem that the equipment video cannot be played completely?

The first artificial intelligence security competition starts. Three competition questions are waiting for you to fight

linux下安装mysql

Strlen introduction, and the difference between sizeof

Domain name (subdomain name) collection method of Web penetration

Introduction to this pointer

Leetcode 18. sum of four numbers
![(manual) [sqli labs27, 27a] error echo, Boolean blind injection, filtered injection](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
(manual) [sqli labs27, 27a] error echo, Boolean blind injection, filtered injection
随机推荐
scipy.stats.chi2
System clock failure of database fault tolerance
App test process and test points
Printf() print char* str
np. unravel_ Index() finds the index value of an element (or group of elements) of the array after being pulled into one dimension. The corresponding index value in the original dimension (or specify
Introduction to testcafe
Real intelligence has been certified by two of the world's top market research institutions and has entered the global camp of excellence
[Sylar] framework chapter -chapter21- environment variable module
flink思维导图
The first artificial intelligence security competition starts. Three competition questions are waiting for you to fight
低代码是开发的未来吗?浅谈低代码平台
X Book Keyword Search
Rendering process, how the code becomes a page (2)
Leetcode 18. sum of four numbers
Basic knowledge of network security - password (I)
Depth traversal and breadth traversal of tree structure in JS
05.01 string
String 0123456789abcdef, what is the number of substrings (not empty and not the same string itself) [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
吉利AI面试题【杭州多测师】【杭州多测师_王sir】
How to quickly locate bugs? How to write test cases?