atcoder#AGC055E. [AGC055E] Set Merging
[AGC055E] Set Merging
Score : points
Problem Statement
You have sets . Initially, for each from to , set contains only integer (that is, ).
You are allowed to perform the following operation:
- Choose any with . Let ー the union of sets and . Then, replace both and with .
You want to perform a finite number of operations above (maybe zero), to get a configuration, in which for all from to . Is it possible to reach this configuration? If it is possible, also find the smallest number of operations needed to reach it.
Constraints
Input
Input is given from Standard Input in the following format:
Output
If it is possible to reach the required configuration, output the smallest number of operations needed to do it. Otherwise, output -1
.
3
1 2
1 2
1 3
-1
It can be proved that it's impossible to obtain the required configuration.
4
1 3
1 4
1 4
2 4
4
We can perform the following sequence of operations:
- Choose , get $S_1 = \{1\}, S_2 = \{2, 3\}, S_3 = \{2, 3\}, S_4 = \{4\}$
- Choose , get $S_1 = \{1, 2, 3\}, S_2 = \{1, 2, 3\}, S_3 = \{2, 3\}, S_4 = \{4\}$
- Choose , get $S_1 = \{1, 2, 3\}, S_2 = \{1, 2, 3\}, S_3 = \{2, 3, 4\}, S_4 = \{2, 3, 4\}$
- Choose , get $S_1 = \{1, 2, 3\}, S_2 = \{1, 2, 3, 4\}, S_3 = \{1, 2, 3, 4\}, S_4 = \{2, 3, 4\}$