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

Автор K0DEL, история, 23 месяца назад, По-английски

I was trying to solve this problem: 1559D1 - Mocha and Diana (Easy Version)

I used the editorial solution for this and just made a small change instead of recursively finding the parent of the node I used an iterative way and it took a lot of time and I want to know why this is happening. The recursive way took 15ms while the iterative way took 764ms.

The editorial submission with recursive method: 189082733 The same submission with iterative method: 189082883

The only difference between the 2 submissions is "getf()" function.

for the recursive approach: int getf(int id, int x){return x == f[id][x] ? x : f[id][x] = getf(id, f[id][x]);}

for the iterative approach: int getf(int id, int x) { while (x != f[id][x]) x = f[id][x]; return x; }

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

»
23 месяца назад, # |
  Проголосовать: нравится -6 Проголосовать: не нравится

Dude, u not deserve yellow color with such obvious questions.

  • »
    »
    23 месяца назад, # ^ |
      Проголосовать: нравится -16 Проголосовать: не нравится

    Dude, you don't deserve purple if you don't know about new year magic.

    • »
      »
      »
      23 месяца назад, # ^ |
        Проголосовать: нравится -16 Проголосовать: не нравится

      Before writing that comment I looked at his rating first. And I mean that he shouldn't disgrace yellow color and should change it to some another.

      So I know about NY magic, don't make fast conclusions.

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

        LMAO. Okay, how are they disgracing yellow color here? It is allowed by CF to do it. Do you want to say everyone who's using new year magic to show higher color disgracing it?

        Also, did you also find out OP's preferred pronouns somewhere? Please do follow your own advice about making fast conclusions.

»
23 месяца назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

You use path compression like in DSU in the recursive approach, where as in the second approach you don't compress paths at all. I.e. if you want to get equavilent solution you should have written the following:

return x == f[id][x] ? x : getf(id, f[id][x]);

The result: 189085171