Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

n =
edges =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Connected components can be tracked by merging endpoints of edges.

Approach

Union-Find over n nodes. Start with count = n. For each edge, union the endpoints; each successful union reduces count.

Why it works

Each component corresponds to one DSU root. Merging edges merges components.

Code
Loading...
Complexity
Time: O(E α(V))
Space: O(V)
Solution
Updated: 2026-02-23

Idea

Connected components can be tracked by merging endpoints of edges.

Approach

Union-Find over n nodes. Start with count = n. For each edge, union the endpoints; each successful union reduces count.

Why it works

Each component corresponds to one DSU root. Merging edges merges components.

Code
Loading...
Complexity
Time: O(E α(V))
Space: O(V)