#abc302f. F - Merge Set
F - Merge Set
Score : points
问题描述
在一块黑板上,有 个包含从 到 的整数的集合 。这里,$S_i = \lbrace S_{i,1},S_{i,2},\dots,S_{i,A_i} \rbrace$。
你可以执行以下操作任意次数(包括零次):
- 选择两个至少有一个公共元素的集合 和 。将它们从黑板上擦除,并在黑板上写下 代替。
这里, 表示包含至少在 或 中的一个元素的所有元素构成的集合。
判断是否能获得一个同时包含 和 的集合。如果可能的话,找出获得该集合所需的最少操作次数。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
On a blackboard, there are sets consisting of integers between and . Here, $S_i = \lbrace S_{i,1},S_{i,2},\dots,S_{i,A_i} \rbrace$.
You may perform the following operation any number of times (possibly zero):
- choose two sets and with at least one common element. Erase them from the blackboard, and write on the blackboard instead.
Here, denotes the set consisting of the elements contained in at least one of and .
Determine if one can obtain a set containing both and . If it is possible, find the minimum number of operations required to obtain it.
Constraints
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
If one can obtain a set containing both and , print the minimum number of operations required to obtain it; if it is impossible, print -1
instead.
Sample Input 1
3 5
2
1 2
2
2 3
3
3 4 5
Sample Output 1
2
First, choose and remove and to obtain .
Then, choose and remove and to obtain .
Thus, one can obtain a set containing both and with two operations. Since one cannot achieve the objective by performing the operation only once, the answer is .
Sample Input 2
1 2
2
1 2
Sample Output 2
0
already contains both and , so the minimum number of operations required is .
Sample Input 3
3 5
2
1 3
2
2 4
3
2 4 5
Sample Output 3
-1
Sample Input 4
4 8
3
1 3 5
2
1 2
3
2 4 7
4
4 6 7 8
Sample Output 4
2
update @ 2024/3/10 08:30:00