#K42822. Magic Items Arrangement

    ID: 27173 Type: Default 1000ms 256MiB

Magic Items Arrangement

Magic Items Arrangement

You are given a set of magic items. Each item has a list of other items it is compatible with. Your task is to determine if it is possible to arrange all the magic items in a straight line in such a way that they are all indirectly or directly connected via compatibility. In other words, check if the underlying compatibility graph is connected.

Formally, you are given an integer (N) representing the number of items, a list of (N) integers (which are not used in the connectivity check) and (N) lines where the (i)-th line contains space-separated integers indicating the items (using 1-based indexing) that are compatible with the (i)-th item. Two items are directly connected if one appears in the other’s compatibility list. An arrangement is possible (output "Yes") if and only if the undirected graph formed by these compatibility relations is connected; otherwise, output "No".

inputFormat

The input is given from standard input (stdin) in the following format:
(N): An integer representing the number of magic items.
A line containing (N) space-separated integers representing the compatibility counts (this information is provided but not used in the connectivity check).
Then follow (N) lines; the (i)-th of these lines contains zero or more space-separated integers, each representing an item (1-indexed) that is compatible with the (i)-th item. If there are no compatible items for an item, the line will be empty.

outputFormat

Print a single line to standard output (stdout): "Yes" if all items are connected (i.e. it is possible to arrange them in a straight line), or "No" otherwise.## sample

4
1 1 2 1
2
3
1 4
3
Yes