Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Province Roots

Learn
Play
Prove
Problem context and objectives
Mission briefing

Puzzle systems engineer · A game mechanic is behaving incorrectly

Merge direct roads into connected regions: The Province Roots.

Brute force may pass the demo but fail when the world fills with players.

How you win

  1. 1Recognize when Union Find component counting matches the clues
  2. 2Keep this true after every move: two cities share a root exactly when processed roads connect them
  3. 3Reach the result within O(n squared alpha(n))

Rules and pressure

  • Target cost: O(n squared alpha(n))
  • State rule: two cities share a root exactly when processed roads connect them
Lesson 1 of 3

Live algorithm trace

Union Find component counting

Complete execution
1 of 6
0
city
1
2

Initialize three singleton city sets.

1parent = range(n); count = n
2scan the upper matrix triangle
3find both city roots
4if roots differ: union them
5count -= 1 after a merge
6return count
parent = [0,1,2]count = 3
Truth to preserve / Cost target
Truth to preserve

two cities share a root exactly when processed roads connect them

Cost target

O(n squared alpha(n))

Begin with one set per city. Every direct road unions two roots; only a successful merge reduces the component count, leaving exactly one representative per province.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead