Skip to content

[pull] master from wisdompeak:master #323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### 3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II

本题的第一个知识点是:在一棵树里,联通u,v,w三个节点的最小子树的权重和,就是`[dist(u,v)+dist(u,w)+dist(v,w)]/2`.

本体的第二个知识点是:在一棵树里,联通x,y两点的路径长度,等于`dist(r,x)+dist(r,y)-2*dist(r,c)`,其中r是整棵树的根节点,c是x和y的LCA(lowest common ancester)。

任意一点到距离根节点的距离dist(r,x)可以通过DFS得到。于是本题的关键点就是求任意两点的LCA,于是就是一个binary list经典题。

我们需要处理得到一个数组up[v][k],表示节点v往上(朝根节点方向)走2^k步能够得到的位置。转移方程就是`up[v][k] = up[up[v][k-1]][k-1]`. 边界条件就是对于一对父子节点a->b,有`up[b][0]=a`.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Solution {
while (1)
{
long lower = prefix * exp;
long upper = prefix * exp + exp + 1;
long upper = prefix * exp + exp - 1;
if (lower > n) break;
if (lower <= n && upper >= n)
{
Expand Down