F - Merge Set 解説 /

実行時間制限: 3 sec / メモリ制限: 1024 MB

配点 : 500500

問題文

黒板に 11 以上 MM 以下の整数からなる集合 NNS1,S2,,SNS_1,S_2,\dots,S_N が書かれています。ここで、Si={Si,1,Si,2,,Si,Ai}S_i = \lbrace S_{i,1},S_{i,2},\dots,S_{i,A_i} \rbrace です。

あなたは、以下の操作を好きな回数(00 回でもよい)行うことが出来ます。

  • 11 個以上の共通した要素を持つ 22 個の集合 X,YX,Y を選ぶ。X,YX,Y22 個を黒板から消し、新たに XYX\cup Y を黒板に書く。

ここで、XYX\cup Y とは XXYY の少なくともどちらかに含まれている要素のみからなる集合を意味します。

11MM が両方含まれる集合を作ることが出来るか判定してください。出来るならば、必要な操作回数の最小値を求めてください。

制約

  • 1N2×1051 \le N \le 2 \times 10^5
  • 2M2×1052 \le M \le 2 \times 10^5
  • 1i=1NAi5×1051 \le \sum_{i=1}^{N} A_i \le 5 \times 10^5
  • 1Si,jM(1iN,1jAi)1 \le S_{i,j} \le M(1 \le i \le N,1 \le j \le A_i)
  • Si,jSi,k(1j<kAi)S_{i,j} \neq S_{i,k}(1 \le j < k \le A_i)
  • 入力は全て整数である。

入力

入力は以下の形式で標準入力から与えられる。

NN MM
A1A_1
S1,1S_{1,1} S1,2S_{1,2} \dots S1,A1S_{1,A_1}
A2A_2
S2,1S_{2,1} S2,2S_{2,2} \dots S2,A2S_{2,A_2}
\vdots
ANA_N
SN,1S_{N,1} SN,2S_{N,2} \dots SN,ANS_{N,A_N}

出力

11MM が両方含まれる集合を作ることが出来るならば必要な操作回数の最小値を、出来ないならば -1 を出力せよ。


入力例 1 Copy

Copy
3 5
2
1 2
2
2 3
3
3 4 5

出力例 1 Copy

Copy
2

まず、{1,2}\lbrace 1,2 \rbrace{2,3}\lbrace 2,3 \rbrace を選んで消し、{1,2,3}\lbrace 1,2,3 \rbrace を追加します。

そして、{1,2,3}\lbrace 1,2,3 \rbrace{3,4,5}\lbrace 3,4,5 \rbrace を選んで消し、{1,2,3,4,5}\lbrace 1,2,3,4,5 \rbrace を追加します。

すると 22 回の操作で 11MM を両方含む集合を作ることが出来ます。11 回の操作では目標を達成できないため、答えは 22 です。


入力例 2 Copy

Copy
1 2
2
1 2

出力例 2 Copy

Copy
0

始めから S1S_11,M1,M を共に含むため、必要な操作回数の最小値は 00 回です。


入力例 3 Copy

Copy
3 5
2
1 3
2
2 4
3
2 4 5

出力例 3 Copy

Copy
-1

入力例 4 Copy

Copy
4 8
3
1 3 5
2
1 2
3
2 4 7
4
4 6 7 8

出力例 4 Copy

Copy
2

Score : 500500 points

Problem Statement

On a blackboard, there are NN sets S1,S2,,SNS_1,S_2,\dots,S_N consisting of integers between 11 and MM. Here, Si={Si,1,Si,2,,Si,Ai}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 XX and YY with at least one common element. Erase them from the blackboard, and write XYX\cup Y on the blackboard instead.

Here, XYX\cup Y denotes the set consisting of the elements contained in at least one of XX and YY.

Determine if one can obtain a set containing both 11 and MM. If it is possible, find the minimum number of operations required to obtain it.

Constraints

  • 1N2×1051 \le N \le 2 \times 10^5
  • 2M2×1052 \le M \le 2 \times 10^5
  • 1i=1NAi5×1051 \le \sum_{i=1}^{N} A_i \le 5 \times 10^5
  • 1Si,jM(1iN,1jAi)1 \le S_{i,j} \le M(1 \le i \le N,1 \le j \le A_i)
  • Si,jSi,k(1j<kAi)S_{i,j} \neq S_{i,k}(1 \le j < k \le A_i)
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN MM
A1A_1
S1,1S_{1,1} S1,2S_{1,2} \dots S1,A1S_{1,A_1}
A2A_2
S2,1S_{2,1} S2,2S_{2,2} \dots S2,A2S_{2,A_2}
\vdots
ANA_N
SN,1S_{N,1} SN,2S_{N,2} \dots SN,ANS_{N,A_N}

Output

If one can obtain a set containing both 11 and MM, print the minimum number of operations required to obtain it; if it is impossible, print -1 instead.


Sample Input 1 Copy

Copy
3 5
2
1 2
2
2 3
3
3 4 5

Sample Output 1 Copy

Copy
2

First, choose and remove {1,2}\lbrace 1,2 \rbrace and {2,3}\lbrace 2,3 \rbrace to obtain {1,2,3}\lbrace 1,2,3 \rbrace.

Then, choose and remove {1,2,3}\lbrace 1,2,3 \rbrace and {3,4,5}\lbrace 3,4,5 \rbrace to obtain {1,2,3,4,5}\lbrace 1,2,3,4,5 \rbrace.

Thus, one can obtain a set containing both 11 and MM with two operations. Since one cannot achieve the objective by performing the operation only once, the answer is 22.


Sample Input 2 Copy

Copy
1 2
2
1 2

Sample Output 2 Copy

Copy
0

S1S_1 already contains both 11 and MM, so the minimum number of operations required is 00.


Sample Input 3 Copy

Copy
3 5
2
1 3
2
2 4
3
2 4 5

Sample Output 3 Copy

Copy
-1

Sample Input 4 Copy

Copy
4 8
3
1 3 5
2
1 2
3
2 4 7
4
4 6 7 8

Sample Output 4 Copy

Copy
2


2023-05-20 (土)
13:42:36 +00:00