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

Автор mahmoud13, история, 3 года назад, По-английски

I got TLE on Test 6 for a problem of today's contest 1900C - Anji's Binary Tree. This is one of my submissions: 234472915. Thank you in advance for your time!

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

»
3 года назад, скрыть # |
 
Проголосовать: нравится +19 Проголосовать: не нравится

u literally go from each node to the root so if u have a chain like this 1 -> 2-> 3-> 4-> 5-> 6 then your algorithm iterate from 6 to 1 and from 5 to 1 and from 4 to 1 and so on which mean u walk : n — 1 + n — 2 + ... + 1 = n * (n — 1) / 2 which can be considered o(n2)

»
3 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I went from every leaf node to node 1. Which complexity should be O(nlogn) in worst . But I also got tle in test case 6

  • »
    »
    3 года назад, скрыть # ^ |
    ← Rev. 3  
    Проголосовать: нравится +3 Проголосовать: не нравится

    I also made the same assumption at first, but I learned what's going on. It is not true that a binary tree has at most log(n) depth, this is a myth. A balanced binary tree is the data structure that does.

    Look at this example:

    View post on imgur.com
    Each node has at most two children, so it is, by definition, a binary tree, but notice that half the nodes are leafs, from that it's easy to realize that going from each leaf to the root converges to O(n^2)
»
3 года назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

Funny thing is that initially all the trees were either not deep, or did not have many leaves. Around 6 hours before the contest I realized that solutions in O(sum_of_leaf_depths) might be able to pass. That is when I added test 6. Here is the description of it:

There is 1 testcase with N=300 000. Letters are assigned randomly. First I build a long path from vertex 1 to vertex 150 000. So basically 1 — 2 — 3 — ... — 150 000. I randomly decided if child will be right or left. Then at 150 000 I add a balanced binary tree with 150 000 verticies. That makes around 75 000 leafs with depth 150 000. Lastly, I shuffled the vertex numbers (but 1 keeps being the root).

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

Go from each leaf, for a subtree if u already have found a minimal answer, don't look at the path for the right subtree. I got AC doing this.

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

i got AC by pruning the branches, check my code below- https://codeforces.me/contest/1900/submission/234642669

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

because you are dump