Hi when I was trying to understand the solution for 293E - Close Vertices by reading AC codes.
I was reading this code 3689358 , but I can't understand the way he stores the tree.
for me I usually use adjecency list to store graphs or trees.
can you tell about this way to store the tree.
thanks for helping.
edit: is it Euler tour? if yes how is way that the code build the Euler tour?
It's adjacency one-linked lists.
head[v]
is id of the first edge adjacent tov
,next[e]
is id of next edge. Look atfor
indfs
— it should make sense.thank you for replying , what is the advantages for this way that cannot adjacency list do?
It is just another way to store adjacency lists using a few pre-allocated arrays instead of dynamic-length arrays or lists. This way is a bit more compact, and perhaps a bit faster. On the other hand, it could lead to less intuitive code and more bugs as a result.
Also note that you can store an arbitrary graph in this way, not only a tree.
I've seen this way in many tree problems so I thought it's for trees. thank you.
I think you're talking about storing list in
vector
, which can be significantly slower than arrays. So, the main advantage is performance. Also you can use this method for precise assessing of memory consumption (on some compilersvector
reserves double of necessary memory, on another — only 1.5 times).in C++, I can build adjecency list without vector and with no extra momery thank you anyway.
It IS adjacency lists. Look at this code.