ByteDance interview question

Graph problem similar to leetcode