2.16 ✅ Union Find

Union Find #

  • Flexibly use the idea of union find, and be proficient with the union find template. There are two implementations of union find in the template: one is the path compression + union by rank version, and the other is the version that calculates the number of elements in each set + the number of elements in the largest set. Both versions have their own use cases. Problems that can use the first type of union find template include: Problem 128, Problem 130, Problem 547, Problem 684, Problem 721, Problem 765, Problem 778, Problem 839, Problem 924, Problem 928, Problem 947, Problem 952, Problem 959, Problem 990. Problems that can use the second type of union find template include: Problem 803, Problem 952. In Problem 803, union by rank and counting the number of elements in a set may cause time limits; without optimization, it will TLE.
  • Union find is an idea. Some problems require flexible use of this idea rather than rigidly applying a template, such as Problem 399. This problem is stringUnionFind, implemented using the union find idea. Here each node is based on strings and maps, rather than simply being implemented with int node numbers.
  • For some problems, rigidly applying the template may make them unsolvable, such as Problem 685. This problem cannot use path compression and union by rank, because the problem involves a directed graph and needs to know each node’s predecessor. If path compression is used, this problem cannot be solved. This problem does not require path compression or union by rank.
  • Flexibly abstract the information given by the problem, reasonably number the given information, use union find to solve the problem, and use maps to reduce time complexity, such as Problem 721 and Problem 959.
  • For problems about maps, bricks, and grids, you can create a special node and union() the bricks or grids around the edges with this special node. Problem 130, Problem 803.
  • Problems that can be solved with union find can generally also be solved with DFS and BFS, but the time complexity will be a bit higher.
No.TitleSolutionDifficultyTimeComplexitySpaceComplexityFavoriteAcceptance
0128Longest Consecutive SequenceGoMediumO(n)O(n)❤️48.5%
0130Surrounded RegionsGoMediumO(m*n)O(m*n)36.7%
0200Number of IslandsGoMediumO(m*n)O(m*n)57.0%
0399Evaluate DivisionGoMediumO(n)O(n)59.6%
0547Number of ProvincesGoMediumO(n^2)O(n)63.7%
0684Redundant ConnectionGoMediumO(n)O(n)62.2%
0685Redundant Connection IIGoHardO(n)O(n)34.1%
0695Max Area of IslandGoMedium71.8%
0721Accounts MergeGoMediumO(n)O(n)❤️56.3%
0765Couples Holding HandsGoHardO(n)O(n)❤️56.6%
0778Swim in Rising WaterGoHardO(n^2)O(n)❤️59.8%
0785Is Graph Bipartite?GoMedium53.1%
0803Bricks Falling When HitGoHardO(n^2)O(n)❤️34.4%
0839Similar String GroupsGoHardO(n^2)O(n)48.0%
0924Minimize Malware SpreadGoHardO(m*n)O(n)42.1%
0928Minimize Malware Spread IIGoHardO(m*n)O(n)❤️42.7%
0947Most Stones Removed with Same Row or ColumnGoMediumO(n)O(n)58.9%
0952Largest Component Size by Common FactorGoHardO(n)O(n)❤️40.0%
0959Regions Cut By SlashesGoMediumO(n^2)O(n^2)❤️69.1%
0990Satisfiability of Equality EquationsGoMediumO(n)O(n)50.5%
1020Number of EnclavesGoMedium65.5%
1202Smallest String With SwapsGoMedium57.7%
1254Number of Closed IslandsGoMedium64.1%
1319Number of Operations to Make Network ConnectedGoMedium62.1%
1579Remove Max Number of Edges to Keep Graph Fully TraversableGoHard53.2%
1631Path With Minimum EffortGoMedium55.7%

Calendar Jun 28, 2026
Edit Edit this page
Total visits:   You are visitor No.
中文