Optiver interview question

A harder leetcode style problem about graphs