Applying Divide and Conquer on Trees: Centroid Decomposition Techniques from the shareOI Repository

The shareOI repository provides comprehensive lecture materials on tree divide-and-conquer algorithms, including a dedicated PDF on centroid decomposition that demonstrates how to recursively decompose trees by their centroids to solve complex path and distance problems in O(N log N) time.

The hzwer/shareoi repository is a curated collection of competitive programming (OI) lecture slides and teaching materials. Among its extensive graph theory resources, the collection offers detailed guidance on divide and conquer on trees, specifically focusing on centroid decomposition techniques that enable efficient solutions for subtree queries and path counting problems.

Understanding Centroid Decomposition in the shareOI Collection

The repository organizes content into categorized directories, with tree-centric divide-and-conquer algorithms residing primarily in the 图论 (Graph Theory) section. These materials provide algorithmic templates rather than executable source code, offering pseudo-code and mathematical proofs suitable for direct implementation in C++, Python, or Java.

Key Resource Files

The following files contain the core methodologies for tree decomposition:

  • 树分治_黄哲威.pdf – Located at 图论/树分治_黄哲威.pdf, this document explains the generic "divide the tree into sub-trees, solve recursively, and combine results" pattern, including linear-time centroid finding algorithms.
  • 树链剖分及其应用_蒋一瑶.pptx – Found at 图论/树链剖分及其应用_蒋一瑶.pptx, this presentation covers heavy-light decomposition as an alternative divide-and-conquer approach for path queries.
  • 分治_lyy.pptx – Located in 基础算法/分治_lyy.pptx, providing general divide-and-conquer methodology applicable to tree recursion design.

Core Concepts: Divide and Conquer on Trees

The centroid decomposition technique documented in the shareOI materials follows a strict recursive pattern that guarantees O(N log N) complexity for tree processing tasks.

The Centroid Selection Algorithm

According to the source materials in 图论/树分治_黄哲威.pdf, a centroid is defined as a node whose removal leaves all remaining connected components with size at most |V|/2. The document provides a two-pass algorithm to locate this node:

  1. First DFS to calculate subtree sizes
  2. Second pass to identify the node where the maximum subtree size is minimized

This centroid serves as the root for the current recursion level, ensuring balanced decomposition regardless of tree shape.

Recursive Subtree Processing

The lecture slides outline a template recursion that processes each component after removing the centroid:

  • Decompose: Remove the centroid and split the tree into independent sub-trees
  • Conquer: Recursively apply the same algorithm to each component
  • Combine: Aggregate results from sub-trees, often using auxiliary data structures like segment trees or hash maps

This pattern is particularly effective for problems involving counting pairs with specific distance constraints or computing subtree aggregates where naive approaches would yield O(N²) complexity.

Practical Implementation Guide

To access the centroid decomposition materials and integrate them into your competitive programming workflow:


# Clone the repository

git clone https://github.com/hzwer/shareoi.git
cd shareoi

# Open the tree divide-and-conquer material on macOS

open 图论/树分治_黄哲威.pdf

# Or on Linux systems

xdg-open 图论/树分治_黄哲威.pdf

Implementation Details from the Source

The PDF 树分治_黄哲威.pdf contains production-ready pseudo-code templates showing:

  • How to maintain a vis array to mark processed centroids and prevent reprocessing
  • Distance calculation methods for paths passing through the current centroid
  • Combination strategies using frequency arrays or binary indexed trees

These templates can be directly translated into executable solutions for problems requiring path queries across entire trees.

Complementary Tree Techniques

The shareOI repository provides adjacent technologies that frequently combine with centroid decomposition:

Heavy-Light Decomposition (树链剖分) – The file 图论/树链剖分及其应用_蒋一瑶.pptx demonstrates how to linearize trees for segment-tree queries, offering an alternative when problems require frequent path updates rather than static decomposition.

Segment Tree Integration – Located at 数据结构/线段树_翁家翌 & 黄哲威.pdf, this resource provides the segment tree implementations often needed after decomposing the tree structure.

Tree Dynamic Programming – The file 动态规划/树形动态规划_朱全民.ppt covers DP techniques that combine with centroid decomposition for complex state transitions on trees.

Summary

  • The hzwer/shareoi repository contains authoritative lecture materials on divide and conquer on trees, specifically centroid decomposition, in the 图论/树分治_黄哲威.pdf file.
  • Centroid decomposition recursively splits trees by their centroids (nodes where no subtree exceeds N/2 nodes), guaranteeing O(N log N) complexity for many path-related problems.
  • The repository provides pseudo-code templates for centroid finding, subtree processing, and result combination that translate directly to C++/Python implementations.
  • Related techniques including heavy-light decomposition and segment trees are available in adjacent files to support complete solution pipelines.

Frequently Asked Questions

What is centroid decomposition used for?

Centroid decomposition is used to solve tree problems involving path queries, distance constraints, and subtree aggregations that would be inefficient with naive traversal. Common applications include counting node pairs with specific distances, finding tree diameters under constraints, and computing path sums, all in O(N log N) time by recursively decomposing the tree at its centroids.

How does the shareOI repository help implement tree divide and conquer?

The shareOI repository provides the file 图论/树分治_黄哲威.pdf, which contains detailed pseudo-code and mathematical explanations of the centroid decomposition algorithm. Unlike generic tutorials, these materials originate from OI competition training and include specific implementation patterns for handling visited nodes, calculating subtree sizes, and combining results from decomposed components.

What is the difference between centroid decomposition and heavy-light decomposition?

Centroid decomposition (图论/树分治_黄哲威.pdf) recursively removes centroids to create a hierarchical decomposition suitable for static queries and counting problems across all paths. Heavy-light decomposition (图论/树链剖分及其应用_蒋一瑶.pptx) linearizes the tree into chains for segment tree processing, optimized for dynamic path queries and frequent updates. The shareOI repository covers both techniques, allowing selection based on whether problems require offline counting or online path modifications.

Where can I find the complete lecture slides for tree algorithms?

The complete collection is available in the hzwer/shareoi GitHub repository. Navigate to the 图论 directory for tree-specific materials including 树分治_黄哲威.pdf (centroid decomposition) and 树链剖分及其应用_蒋一瑶.pptx (heavy-light decomposition). The README.md file at the repository root provides a categorized index of all available algorithmic resources.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →