Optiver Interview Question

A graph problem like a leetcode one