We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
All input edges are a graph edges in a random order and vertexes are also in a random order [3,4] vs [4,3], so we create 'treeEdgesPossible' with all possible tree edges(parent to child). We need to know actual tree edges which are directed so we need to choose [3,4] or [4,3]
'while(!bfsQueue.empty())' is very important part - here we reconstruct our tree using BFS. Main point is that the tree root node is guaranteed to be a first edge at the problem's input. So it is our start point - tree root node
we store our found parent to child tree edges at 'treeEdgesActual'
also very importent optimization logic is at 'values.at(treeEdge.first) += values.at(treeEdge.second);'. Here we prepare values to find the minimum absolut difference.
Finally we simulate a cut at each tree node starting from the root(top). At the previous step we precomputed sub-tree sums at each cut.
Cut the Tree
You are viewing a single comment's thread. Return to all comments →
C++ (more at https://github.com/IhorVodko/Hackerrank_solutions , feel free to give a star:) )