news 2026/7/15 4:43:14

DeepSeek LeetCode 3553. 包含要求路径的最小带权子图 II JavaScript实现

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
DeepSeek LeetCode 3553. 包含要求路径的最小带权子图 II JavaScript实现

核心思路

本题给定的是无向带权树,树中任意两点间有且仅有一条简单路径。对于每个查询 (src1, src2, dest),包含两条路径的最小连通子图就是这两条路径的并集。

关键公式:三条路径 (src1→src2)、(src1→dest)、(src2→dest) 的并集总权重为:

```
(dist(src1, src2) + dist(src1, dest) + dist(src2, dest)) / 2
```

因为并集中的每条边在三段两两距离中恰好被计算两次。

预处理使用二进制提升(Binary Lifting) 求LCA,单次查询 O(log n)。

完整 JavaScript 实现

```javascript
/**
* @param {number[][]} edges - 边列表 [u, v, w]
* @param {number[][]} queries - 查询列表 [src1, src2, dest]
* @return {number[]} - 每个查询的最小总权重
*/
function minimumWeight(edges, queries) {
const n = edges.length + 1;
const graph = Array.from({ length: n }, () => []);

// 构建无向图
for (const [u, v, w] of edges) {
graph[u].push([v, w]);
graph[v].push([u, w]);
}

// DFS 预处理 parent 和 depth
const parent = Array(n).fill([-1, 0]);
const depth = Array(n).fill(0);

function dfs(node, par, parWeight, d) {
depth[node] = d;
parent[node] = [par, parWeight];
for (const [nei, weight] of graph[node]) {
if (nei === par) continue;
dfs(nei, node, weight, d + 1);
}
}
dfs(0, -1, 0, 0);

const lca = new LCA(n, parent, depth);
const ans = [];

for (const [src1, src2, dest] of queries) {
const d1 = lca.getDist(src1, src2);
const d2 = lca.getDist(src1, dest);
const d3 = lca.getDist(src2, dest);
ans.push((d1 + d2 + d3) / 2);
}

return ans;
}

class LCA {
constructor(n, parent, depth) {
this.maxDepth = Math.ceil(Math.log2(n)) + 1;
this.depth = depth;

// up[k][v] = v 的 2^k 级祖先
this.up = Array.from({ length: this.maxDepth }, () => Array(n).fill(-1));
// sum[k][v] = v 到其 2^k 级祖先的路径权重和
this.sum = Array.from({ length: this.maxDepth }, () => Array(n).fill(0));

// 初始化第 0 层
for (let v = 0; v < n; v++) {
this.up[0][v] = parent[v][0];
this.sum[0][v] = parent[v][1];
}

// 递推计算更高层
for (let k = 1; k < this.maxDepth; k++) {
for (let v = 0; v < n; v++) {
const mid = this.up[k - 1][v];
if (mid !== -1) {
this.up[k][v] = this.up[k - 1][mid];
this.sum[k][v] = this.sum[k - 1][v] + this.sum[k - 1][mid];
}
}
}
}

// 获取 LCA
getLCA(a, b) {
if (this.depth[a] > this.depth[b]) [a, b] = [b, a];

// 将 b 提升到与 a 同深度
let diff = this.depth[b] - this.depth[a];
for (let k = 0; k < this.maxDepth; k++) {
if ((diff >> k) & 1) {
b = this.up[k][b];
}
}

if (a === b) return a;

// 同时提升
for (let k = this.maxDepth - 1; k >= 0; k--) {
if (this.up[k][a] !== this.up[k][b]) {
a = this.up[k][a];
b = this.up[k][b];
}
}

return this.up[0][a];
}

// 获取 a 到 b 的路径权重和
getDist(a, b) {
const lca = this.getLCA(a, b);
return this._getUpSum(a, this.depth[a] - this.depth[lca]) +
this._getUpSum(b, this.depth[b] - this.depth[lca]);
}

// 获取节点 v 到其 k 级祖先的路径权重和(内部方法)
_getUpSum(v, k) {
let sum = 0;
for (let i = 0; i < this.maxDepth; i++) {
if ((k >> i) & 1) {
sum += this.sum[i][v];
v = this.up[i][v];
}
}
return sum;
}
}

// 测试用例
console.log(minimumWeight(
[[0,1,2],[1,2,3],[1,3,5],[1,4,4],[2,5,6]],
[[2,3,4],[0,2,5]]
)); // [12, 11]

console.log(minimumWeight(
[[1,0,8],[0,2,7]],
[[0,1,2]]
)); // [15]
```

复杂度分析

项目 复杂度
预处理(DFS + 二进制表) O(n log n)
单次查询 O(log n)
空间 O(n log n)

其中 n 为节点数,m 为查询数。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/15 4:42:40

LibYAML超全教程-C语言YAML解析与生成

一、前言&#xff1a;为什么要学 LibYAML&#xff1f; 1.1 YAML 配置文件优势 在服务开发、嵌入式开发中&#xff0c;配置文件是核心基础组件。相较于传统 INI、JSON、XML&#xff0c;YAML 凭借极简语法、高可读性、天然支持注释、多层嵌套结构&#xff0c;成为目前工业级主流…

作者头像 李华
网站建设 2026/7/15 4:41:23

C++ STL容器选择指南:vector与list的性能对比与应用场景

1. 从“默认选择”谈起&#xff1a;为什么vector是C STL的宠儿&#xff1f;在C标准模板库&#xff08;STL&#xff09;的序列式容器家族里&#xff0c;vector和list是两位性格迥异的成员。Scott Meyers在《Effective STL》里那句“vector是默认应该使用的序列容器类型”&#x…

作者头像 李华
网站建设 2026/7/15 4:41:15

TPS7A54 4A超低噪声LDO设计:从原理到PCB布局的工程实践

1. 项目概述&#xff1a;为什么我们需要一颗4A、低噪声的LDO&#xff1f;在电源设计的江湖里&#xff0c;低压差线性稳压器&#xff08;LDO&#xff09;一直扮演着“净水器”的角色。它的任务&#xff0c;就是把上游开关电源&#xff08;DC/DC&#xff09;产生的“湍急河水”般…

作者头像 李华
网站建设 2026/7/15 4:40:58

BaiduPCS-Go:高性能命令行网盘管理工具的5大核心技术解析

BaiduPCS-Go&#xff1a;高性能命令行网盘管理工具的5大核心技术解析 【免费下载链接】BaiduPCS-Go iikira/BaiduPCS-Go原版基础上集成了分享链接/秒传链接转存功能 项目地址: https://gitcode.com/GitHub_Trending/ba/BaiduPCS-Go BaiduPCS-Go是一款基于Go语言开发的百…

作者头像 李华
网站建设 2026/7/15 4:39:43

HoRain云--LangChain 个人知识库问答系统

&#x1f3ac; HoRain云小助手&#xff1a;个人主页 &#x1f525; 个人专栏: 《Linux 系列教程》《c语言教程》 ⛺️生活的理想&#xff0c;就是为了理想的生活! ⛳️ 推荐 前些天发现了一个超棒的服务器购买网站&#xff0c;性价比超高&#xff0c;大内存超划算&#xff01;…

作者头像 李华
网站建设 2026/7/15 4:39:19

C++ std::bind 参数绑定机制详解:从原理到实战应用

1. 项目概述在C的现代编程实践中&#xff0c;函数对象&#xff08;Function Object&#xff09;和回调机制是构建灵活、可复用代码的基石。无论是事件驱动系统、异步任务调度&#xff0c;还是实现策略模式&#xff0c;我们常常需要将某个函数与其部分参数预先“绑定”起来&…

作者头像 李华