![]() |
VOOZH | about |
Given an N-ary tree with weighted edge and Q queries where each query contains two nodes of the tree. The task is to find the maximum weighted edge in the simple path between these two nodes.
Examples:
Naive Approach: A simple solution is to traverse the whole tree for each query and find the path between the two nodes.
Efficient Approach: The idea is to use binary lifting to pre-compute the maximum weighted edge from every node to every other node at distance of some
. We will store the maximum weighted edge till
level.
and
where
We'll do a depth-first search to find all the parents at
distance and their weight and then precompute parents and maximum edges at every
distance.
Below is the implementation of the above approach:
1 5 5
Time Complexity: O(N*logN).
Auxiliary Space: O(N*logN).