Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Graphs/Minimum Spanning Tree

Technique · CP 2400+

Kruskal Reconstruction Tree

During Kruskal, every successful union creates a new node whose value is the edge weight and whose children are the two roots. The bottleneck weight between u and v is then their LCA's value, and 'vertices reachable from v using edges <= x' is a single subtree.

Learn firstMinimum Spanning TreeUnion-Find (Disjoint Set)Binary Lifting & LCAEuler Tour / Tree Flattening
0%
mastery
0/4 solved
Practise nextGraph and Queries
IdeaPracticePitfalls

Recognise it

The signal

minimum possible maximum edge on a u-v path, many queries

  1. 1Brute force

    BFS per query restricted to edges <= x: O(q(n + m)).

  2. 2The observation

    Re-traversal per query; an offline DSU cannot answer questions about the reachable set's contents online.

  3. 3Why it works

    Node values never decrease towards the root. The set reachable from v under <= x is the leaf set of v's highest ancestor with value <= x.

Reach for it when

  • +from v, using only edges <= x, ask something about the reachable set (max value, count, k-th)
  • +an online version of a 'sort edges and union' offline problem

Not when

  • ×only yes/no thresholds, with offline processing allowed (sort queries + DSU is simpler)

Practice

Next

Graph and Queries

0/4 solved
  • New Roads QueriesCSESMedium
  • Graph and QueriesCodeforces2600
  • Qpwoeirut and VerticesCodeforces2300
  • Groceries in Meteor TownCodeforces3100

Pitfalls

Misconceptions

  • !it is not the MST itself: the KRT has 2n - 1 nodes and the original vertices are its leaves
Go deeper2 combinations · resources

Combinations

  • Kruskal Reconstruction Tree+Binary Lifting & LCA

    the minimum possible maximum edge between u and v (or the first time they connect) for many pairs

    Kruskal Reconstruction Tree:
    turns each union into a node valued by the edge weight
    Binary Lifting & LCA:
    LCA queries, or jumping to the highest ancestor with value <= x

    Qpwoeirut and VerticesNew Roads Queries

  • Kruskal Reconstruction Tree+Euler Tour / Tree Flattening+Segment Tree / Fenwick (BIT)

    edges deleted over time, plus queries for the max value in v's current component

    Kruskal Reconstruction Tree:
    the threshold-reachable set is a subtree
    Euler Tour / Tree Flattening:
    that subtree is a contiguous leaf range
    Segment Tree / Fenwick (BIT):
    range max with point delete over the leaf order

    Graph and Queries

Resources

  • Graph Theory playlistWilliam Fiset · video
  • Graph TraversalUSACO Guide · course
  • Disjoint Set Unioncp-algorithms · article
  • Dijkstracp-algorithms · article
  • Topological sortingcp-algorithms · article
  • Shortest paths visualizerVisuAlgo · tool
  • Graph cheatsheetTech Interview Handbook · article
TodayReviewPatternsProblems