Problem 1496 is solved by disjoint-set