Mall
Time limit1sMemory limit256 MB
Assign each product to a shop that sells it, and order the shops so that no shop is entered after a product it sells was already bought elsewhere.
- Level
Medium7 of 10
- Topics
- Graph, Greedy, Topological sort, Implementation
- Solved
- No attempts yet
Problem
Byton's parents have sent him to a nearby shopping mall to buy the products on the list, numbered through . He loves shopping, so he plans to visit every shop in the mall, each of them exactly once. Byton will visit the shops in some order, and in some of them he will buy some products from the list that he has not bought yet.
As you can guess, some products may be available in multiple different shops. Unfortunately, Byton is a bit paranoid: he fears random security checks very much. Therefore, he would like to avoid an awkward situation in which he enters a shop that sells a product that he has already purchased somewhere else.
Can you find a strategy of visiting all the shops and buying products, which will allow Byton to avoid awkward situations with security guards?
Input
The first line of the input contains two integers (): the number of shops in the mall and the number of products Byton needs to buy, respectively. The next lines describe shops in the mall; the -th of them describes the -th shop. Each description begins with a number () denoting the number of products of Byton's interest available in the -th shop. Then, integers, each between and , follow in ascending order. Each of them denotes a product from Byton's list.
Output
If there does not exist a correct strategy of shopping, you should output a single word NO. Otherwise, the first line of the output should contain the word YES. The second line of the output should contain distinct integers ranging from to : the order of shops visited by Byton. The last, third line should contain integers ranging from to ; the -th of them indicates the shop in which Byton should buy the product . If there are multiple solutions, output any of them.
Hint
First, Byton should go to shop , not buying anything. Then, he should go to shop and buy the products and . Next, he can buy product in shop and finally buy product in shop .