0PricingLogin
Competitive Programming Academy · Lesson

Union by Rank and Components

Keep trees flat and count groups.

Union Can Be Lazy

Plain union just hangs one root under another. Done carelessly, it can build a tall, slow tree, so we need a smarter way to merge roots.

The Big Idea

Union by rank always attaches the shorter tree under the taller one. Keeping trees shallow makes every later find faster. 📏

All lessons in this course

  1. DSU with Path Compression
  2. Union by Rank and Components
  3. Kruskal's Minimum Spanning Tree
  4. Prim's MST with a Heap
← Back to Competitive Programming Academy