Files
algorithm-template/binary-lifting/2846. minimum-edge-weight-equilibrium-queries-in-a-tree.cpp

100 lines
3.9 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* There is an undirected tree with n nodes labeled from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ui, vi, wi] indicates that there is an edge between nodes ui and vi with weight wi in the tree.
You are also given a 2D integer array queries of length m, where queries[i] = [ai, bi]. For each query, find the minimum number of operations required to make the weight of every edge on the path from ai to bi equal. In one operation, you can choose any edge of the tree and change its weight to any value.
Note that:
Queries are independent of each other, meaning that the tree returns to its initial state on each new query.
The path from ai to bi is a sequence of distinct nodes starting with node ai and ending with node bi such that every two adjacent nodes in the sequence share an edge in the tree.
Return an array answer of length m where answer[i] is the answer to the ith query.
*/
#include <vector>
#include <array>
#include <bit>
#include <bitset>
#include <ranges>
using namespace std;
class Solution {
private:
vector<vector<int>> predecessor;
vector<int> depth;
vector<vector<array<int, 26>>> counter;
public:
vector<int> minOperationsQueries(int n, vector<vector<int>>& edges, vector<vector<int>>& queries) {
int m = bit_width((unsigned)n);
counter.resize(n, vector<array<int, 26>>(m)); // counter[k][i][j] -> 从结点k向上跳2^i权重j出现得次数权重取值范围1~26
predecessor.resize(n, vector<int>(m, -1));
depth.resize(n, 0);
vector<vector<pair<int, int>>> graph(n);
for (const auto& edge : edges) {
int u = edge[0], v = edge[1], w = edge[2] - 1;
graph[u].emplace_back(v, w);
graph[v].emplace_back(u, w);
}
auto dfs = [&](auto&& dfs, int x, int fa) -> void {
predecessor[x][0] = fa;
for (auto [y, w] : graph[x]) {
if (y != fa) {
counter[y][0][w] = 1;
depth[y] = depth[x] + 1;
dfs(dfs, y, x);
}
}
};
dfs(dfs, 0, -1);
for (int i = 1; i < m; i++) {
for (int x = 0; x < n; x++) {
if (int p = predecessor[x][i - 1]; p != -1) {
predecessor[x][i] = predecessor[p][i - 1];
for (int j = 0; j < 26; j++) {
counter[x][i][j] = counter[x][i - 1][j] + counter[p][i - 1][j];
}
}
}
}
vector<int> ans;
for (auto& query : queries) {
int u = query[0], v = query[1];
int path_len = depth[u] + depth[v];
array<int, 26> cur_counter = {0};
if (depth[u] > depth[v]) {
swap(u, v);
}
for (int k = depth[v] - depth[u]; k; k &= k - 1) {
int i = countr_zero(unsigned(k));
int p = predecessor[v][i];
for (int j = 0; j < 26; j++) {
cur_counter[j] += counter[v][i][j];
}
v = p;
}
if (u != v) {
for (int i = m - 1; i >= 0; i--) {
int pu = predecessor[u][i], pv = predecessor[v][i];
if (pu != pv) {
for (int j = 0; j < 26; j++) {
cur_counter[j] += counter[u][i][j] + counter[v][i][j];
}
u = pu, v = pv;
}
}
for (int j = 0; j < 26; j++) {
cur_counter[j] += counter[u][0][j] + counter[v][0][j];
}
u = predecessor[u][0];
}
path_len -= depth[u] * 2;
ans.push_back(path_len - ranges::max(cur_counter));
}
return ans;
}
};