use dfs, component, floyd algorithm solve it, last sample TLE, Todo, refer to others' elegant code