#K64212. Minimum Playlist

    ID: 31926 Type: Default 1000ms 256MiB

Minimum Playlist

Minimum Playlist

You are given a music library with n songs and a list of the user's p favorite genres. Each song is described by a list of integers: the first integer indicates the number of genres associated with that song, and the following integers represent the genres themselves. Your task is to determine the minimum number of songs required to form a playlist such that the union of the genres from these songs covers all of the user's favorite genres. If it is impossible to cover all favorite genres using any combination of songs, output 0.

Note: All inputs are read from standard input, and the result is printed to standard output.

Input Format:

  • The first line contains two integers n and p, where n is the number of songs, and p is the number of favorite genres.
  • The second line contains p integers representing the user's favorite genres.
  • The following n lines each describe a song. Each song starts with an integer t denoting the number of genres for that song, followed by t integers representing the genres.

Output Format:

  • Output a single integer representing the minimum number of songs that cover all favorite genres, or 0 if no such selection exists.

Mathematical Formulation:

Let F be the set of favorite genres and for each song i, let Si be its set of genres. You need to find the smallest subset I of songs such that:

[ \bigcup_{i \in I} S_i \supseteq F ]

inputFormat

The input is given via standard input. The first line contains two space-separated integers n and p. The second line contains p space-separated integers representing the favorite genres. The next n lines each start with an integer t (the number of genres for that song) followed by t space-separated integers indicating the genres of that song.

outputFormat

Print a single integer to standard output — the minimum number of songs required to cover all the favorite genres. If it is impossible, print 0.

## sample
5 3
2 3 5
3 1 2 3
2 2 5
1 3
2 4 5
3 1 2 5
2