Binary Lifting for LCA Queries
Directory
Preprocessing
LCA
External Function Version
Preprocessing
void solve() {
int n, k;
cin >> n >> k;
vector<vector<int>> adj(n + 1);
for (int i = 1; i <= n - 1; ++i) {
int x, y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
vecto ...
Posted on Tue, 08 Sep 2026 16:35:03 +0000 by FadeOut79
Dynamic Programming: Integer Break and Unique Binary Search Trees
343. Integer Break
Problem Link: LeetCode 343 - Integer Break
Given an integer n, break it into at least two positive integers, where the sum equals n. Return the maximum product possible from these integers.
Example:
Input: 2
Output: 1
Explanation: 2 = 1 + 1, 1 × 1 = 1
Apprroach
This is a classic dynamic programming problem that can be solv ...
Posted on Sun, 06 Sep 2026 16:35:09 +0000 by kontesto
Prufer Sequences
Tree to Prufer Sequence
Find the leaf node with the smallest label, and add its parent to the sequence.
Delete that leaf node.
Repeat the above operations until a sequence of length (n-2) is obtained.
The reverse process reconstructs the tree.
A Prufer sequence establishes a bijection between the spanning trees of a complete graph with (n) ve ...
Posted on Sun, 06 Sep 2026 16:21:39 +0000 by mr. big
JavaScript Frontend Data Structures: Trees
Tree Definiiton and Characteristics
A tree is a data structure with n (n ≥ 0) finite nodes in a hierarchical relationship. Called a "tree" as it resembles an upside - down tree (root up, leaves down). Key traits:
A node may have 0+ child nodes.
The root node has no parent.
Every non - root node has exactly one parent.
Except the root ...
Posted on Fri, 21 Aug 2026 16:05:18 +0000 by sks1024
Maximizing Sum of Minimum Edge Weights on Tree Paths
Given a tree with n nodes (≤ 10⁶) and weighted edges, define Min(x, y) as the minimum edge weight along the unique path between nodes x and y. The goal is to compute:
max_{r=1}^n Σ_{v ≠ r} Min(r, v)
A naive approach would compute the sum for each root r by traversing all paths, yielding O(n²) complexity — infeasible for large n.
Instead, revers ...
Posted on Sun, 16 Aug 2026 16:36:12 +0000 by Dasndan
Dynamic Programming on Trees with Heavy-Light Decomposition and Matrix Multiplication
Weighted Independent Set with Point Updates on a Tree
Consider a rooted tree where every node carries a weight. We must support point-weight modifications and after each change report the maximum-weight independent set of the entire tree. Node count and operation count are up to (10^5), weights are bounded in absolute value by (10^2).
Standard ...
Posted on Tue, 11 Aug 2026 16:21:13 +0000 by verano
Solutions for CodeForces Round 656 Division 3
A - Three Pairwise Maximums
Given three pairwise maximum values, determine if they can be derived from three positive integers. The solutoin involves sorting the input values and verifying consistency conditions.
#include <iostream>
#include <algorithm>
using namespace std;
void solve() {
int nums[3];
cin >> nums[0] & ...
Posted on Fri, 07 Aug 2026 17:03:21 +0000 by lewisstevens1
Tree Problem Summary
Introduction
During the summer vacation, I systematically studied various operations on trees through Teacher Tuo's sharing and discovered many useful techniques, which I now summarize.
Teacher Tuo is amazing!
One
Problem Type: Batch processing queries of the form f(dep(lca(x,y))), where f(x) is a function of x.
Approach: First perform tree ...
Posted on Sat, 01 Aug 2026 16:21:20 +0000 by D_tunisia
Recovering and Validating Binary Search Trees
Recovering a Swapped Binary Search Tree
A binary search tree (BST) has two of its nodes swapped by mistake. The task is to restore the tree to its correct BST form without altering its structure. The challenge is to achieve this with constant space complexity (O(1) space), avoiding the use of a full in-order traversal list.
Approach
The key obs ...
Posted on Thu, 14 May 2026 13:38:34 +0000 by John_S
Greedy Algorithm: Minimum Cameras to Monitor a Binary Tree
Greedy Algorithm: Minimum Cameras to Monitor a Binary Tree
Given a binary tree, we need to place cameras on nodes such that every node in the tree is monitored. A camera placed on a node monitors itself, its parenet, and its immediate children. Determine the minimum number of cameras required.
Approach
We can solve this problem using a greedy a ...
Posted on Wed, 13 May 2026 14:26:44 +0000 by TPerez