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

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

Link :https://online.acmicpc.org/problems/ceiling

I had an idea to solve this problem in O(n^2) by just presenting tree as String.For example we will have 2 trees(both used to compare each other).If we have List : 5 6 3 9 the string representation of this tree wouls be "RLRR" which means they were added in the tree in following order.After i build both trees i would check if they have same property(amount of L's and R's) if that is the case i would mark those trees the same.

My soulution is giving WA on 7th case and i wonder what it is?

Thanks

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

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

If the list is 5 3 6 9 the string representation according to u would be "LRRR" but the same tree is formed

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

UPDATE : I just did paralell DFS traversal and it passed but i still cant figure it out why string representation wont work...

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

I solved the problem this way:

The set of all possible trees is infinite and countable so there is a bijection between this set and the natural numbers. So you can build and then "code" every tree using this bijection and use a set to store how many different codes were found.

The coding is given recursively:

code(v) = 0 if v is leaf node

code(v) = f(v->left,v->right) otherwise

f(a,b) should give some unique natural number identifying this pair of subtrees

My implementation: http://pastebin.com/wE4TWs05

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

I used hashing and a single tree to guide hashing process and which indices to update.

The main idea was to keep an id field in node structure to define which tree a node belongs to, so when we insert a new element, if current id conflicts with the new id then we just change node's key and insertion is done.

For hashing, I used i*2+1, i*2+2 to move along indices for left and right directions to avoid collisions

Code