#C4105. Open All Chests

    ID: 47607 Type: Default 1000ms 256MiB

Open All Chests

Open All Chests

You are given n chests numbered from 1 to n. Each chest may contain keys to other chests. Initially, you are provided with a starting key which opens the corresponding chest. When you open a chest, you obtain all the keys contained in it. Note that a chest can be opened only once. Your task is to determine whether it is possible to open all the chests.

The process can be thought of as a graph traversal where nodes are chests and edges are keys that allow you to move from one chest to another. Formally, if you start with key \( s \) and each chest \( i \) contains keys to other chests represented in a list, you need to verify if all chests \( 1, 2, \dots, n \) can be reached.

inputFormat

The first line of the input contains two integers \( n \) and \( s \) where \( n \) is the number of chests and \( s \) is the starting key (representing the chest you can open initially).

This is followed by \( n \) lines. The \( i^{th} \) of these lines starts with an integer \( k_i \) representing the number of keys inside chest \( i \), followed by \( k_i \) integers which are the keys contained in chest \( i \). Note that the chests are indexed from 1 to \( n \).

outputFormat

Output a single line containing "YES" if it is possible to open all chests, or "NO" otherwise.

## sample
4 2
0
1 3
2 1 4
1 4
YES