A Reverse DSU for Leetcode 3613.

Правка en1, от happywobbuffet, 2025-07-14 22:35:18

Hello, I was solving this Leetcode problem yesterday.

I'll rewrite the problem statement once again for convenience:

Problem Statement
Examples
Constraints

It's a cool problem. I would suggest to try solving by your own.

Onto the main part now.

I came across this nice solution where you first sort the edges, in increasing order of their edge weights.

This is followed by greedily picking edges from smallest to largest and using DSU to keep count of connected components so far.

While reading through the solution, I came up with an idea that works in reverse:

  • Build the entire graph using the edge information. Initially it is guaranteed to be 1 connected component.

  • Sort the edges in decreasing order of their edge weights (large to small).

  • Greedily remove the edges (largest to smallest) and for every edge removed edge, check if connected components reaches upto $$$k$$$ or not.

Теги graph-theory, union-find, disjoint-set, connected-components

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский happywobbuffet 2025-07-15 07:49:31 1425 Tiny change: 'anding. \n- How do' -> 'anding. \n\n- How do' (published)
en1 Английский happywobbuffet 2025-07-14 22:35:18 2845 Initial revision (saved to drafts)