Connectivity (DSU and DFS)

You know DFS and Union-Find basics. Now decide which tool fits each problem. Solve Accounts Merge and learn when DSU beats DFS for connectivity.

27 lessons
165 min
Codeforces: 1600-2000LeetCode: 1700-2000

Lessons

1. Introduction to Connectivity

Can A reach B? Same component?

4m

2. Union-Find with Path Compression and Rank

The data structure

4m

3. DSU vs DFS

Choosing the right tool

4m

4. Problem - Number of Islands II

LC 305 - land appears over time

3m1 problems

5. Number of Islands II - Why Naive Fails

Add land, check 4 neighbors

4m

6. Number of Islands II - Setting Up Union-Find

Union-Find on m×n grid

4m

7. Number of Islands II - Transition

Mark land, union neighbors, track count

4m

8. Number of Islands II - Base Cases

Out of bounds, duplicates

4m

9. Number of Islands II - Implementation

idx = r×n + c conversion

5m1 problems

10. Number of Islands II - Time and Space

K positions, O(K·α(mn))

3m

11. Problem - Graph Valid Tree

LC 261 - is this a valid tree?

4m1 problems

12. Graph Valid Tree - Solution

n-1 edges and no cycle

4m

13. Graph Valid Tree - Why This Works

Edge count + Union-Find cycle check

3m

14. Quiz: Union-Find Applications

DSU vs DFS tradeoffs

3m1 problems

15. Quiz: Cycle Detection

When does extra edge create cycle?

3m1 problems

16. Common Mistakes in Connectivity

Path compression, union by rank

4m

17. Problem - New Roads Queries

CSES - connectivity queries

10m1 problems

18. New Roads Queries - Solution

The solution

10m

19. Problem - Number of Good Paths

LC 2421 - value paths

10m1 problems

20. Number of Good Paths - Solution

The solution

10m

21. Problem - Rank Transform of Matrix

LC 1632 - matrix ranking

10m1 problems

22. Rank Transform of Matrix - Solution

The solution

10m

23. Problem - Asya and Kittens

CF 1131F - merging groups

10m1 problems

24. Asya and Kittens - Solution

The solution

10m

25. Problem - Cows and Snacks

CF 1209D - losing snacks

10m1 problems

26. Cows and Snacks - Solution

The solution

10m

27. Section Recap

From accounts merge to edge limits

5m

Practice Problems

Ready to start learning?

Access all 27 lessons with interactive content and progress tracking.