Editorial for VPEX P4 - Etopika
Submitting an official solution before solving the problem yourself is a bannable offence.
Subtask 1
Calculate the distance between nodes using BFS or DFS. Calculate the distance travelled for every order of visiting the nodes and find the minimum.
Key Concepts Required: Graph Theory, Brute Force
Time Complexity:
Subtask 2
Precompute the distance between any two nodes by doing a DFS or BFS from each node.
If nodes and had bananas yesterday and nodes and had bananas today, you have four options today:
- to to
- to to
- to to
- to to
If distance travelled up to day and ending on node , and ending on node ,
And the answer is .
Because the monkey starts at node , .
Key Concepts Required: Graph Theory, Dynamic Programming
Time Complexity:
Subtask 3
This solution is similar to the solution to subtask 2. Instead of precomputing the distance between all pairs of nodes, we can find the distance between any two nodes by using their lowest common ancestor (LCA).
Key Concepts Required: Graph Theory, Dynamic Programming, Sparse Table
Time Complexity:
Comments