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

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

Hello codeforces!

I recently got interested in the LCA problem and read a lot about it. I learned three methods for computing LCA queries:

  • Using Sparse Table (But i didn't like that approach since the 2-d array gives me outofmemory exception when number of nodes is 10^5 )

  • Transforming my rooted tree into an array and apply segment trees on it. This method works well for me but takes much too long to code.

  • Using Union-Find data structure.

Which approach do you use and why? Thanks!

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

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

I use ST. I think it's the most convinient one.

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

If the time and memory limits allow it, I would go for binary lifting method. Otherwise, I will most likely code heavy-light decomposition which gives O(N) preprocessing, O(N) memory and O(logN) per query :)

  • »
    »
    8 лет назад, # ^ |
    Rev. 3   Проголосовать: нравится -16 Проголосовать: не нравится

    Nice! but binary lifting is slow with a query of O(log^2 n)

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

      Actually it's O(logN), what makes it worse than HLD is the preprocessing time and memory complexity both of O(NlogN). But it's much easier to code so that's my first choice if the memory and time limit allow it, as I said :)

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

I use sparse table (parent[lgN][N]) as it's easy to code and doesn't require any additional knowledge (segment trees).
BTW, if N=100000, 4*17*100000 = 6.8 MB. Where is your MLE?

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

    how is it [logN][N] ? My implementation use parent[N][N] Can you provide a code of your approach?

    Thanks!

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

    You can implement HLD without Segment Tree, and it works for LCA problems. Easier to code for me. Which one do you find faster in practice ??