Editorial for An Animal Contest 3 P5 - Monkey Canoe
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
Notice that a path from a land cell in row to row is a concatenation of multiple edges each with various lengths. An edge of length exists between two cells of land if both cells are in the same row/column and the space between them on that row/column is occupied by puddle cells.
All edges (with a maximum of ) can be built by considering rows and columns separately and connecting each land cell to the subsequent cell in the corresponding row/column being iterated.
For ease of storing edges, we map each cell with a distinct id.
Process the edges in decreasing order by length and maintain a bi-directional graph of active edges. After all edges of a certain length are added to the graph, we can BFS from all land cells in row and track land which cells in row are visited (again, using only edges with length ). Note that there is a maximum of different edge lengths.
Note: To find the cells in row that can be reached with a canoe size being infinitely large, we process all edges with , and BFS to find the land cells holding this special case.
Time Complexity:
Subtask 2
Using the cell ids, maintain a DSU to query for which cells are connected after a certain number of edges have been processed.
For infinitely large canoe sizes, process the edges with , adding each to the DSU and checking which land cells in row share a set with land cells in row in time.
For the remaining edge lengths, process them in decreasing order. After all edges of a certain length are added to the DSU, we can repeat the check for finding which land cells in row can be connected with a land cell in row using edge lengths . Note that there are a maximum of different edge lengths.
Time Complexity:
Comments
When Testing, I have an alternate solution where I build the graph, and run a variation of dijkstra where I maximize the minimum edge.
Another approach: Build any maximum spanning tree, create a new node connecting first row and run a dfs from it: "minimum edge in the path to a last row cell is the answer".
Complexity: