Блог пользователя skpro19

Автор skpro19, история, 7 лет назад, По-английски

I am solving this DIV 2C. This is my submission which got accepted.

My question is this:

Why isn't updating the parent of a node, an O(n) operation ?

  • Проголосовать: нравится
  • -8
  • Проголосовать: не нравится

»
7 лет назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

You are constructing the disjoint set using path compression. i.e. When you look for the father of a node, each node on the path to the root remember the father, so the complexity is not linear per query. If you want to be even more sure about the complexity you should do rank compression, i.e. remember for each tree their height, and make the shorter tree child of the taller tree (the one with highest height). The overall complexity per query is ackerman inverse per query (amortized).

More info about Disjoint Set.