Data Structures Are Really the Best
InterviewTime limit1sMemory limit1024 MB
Given M stacks of distinct books numbered 1 to N, decide whether repeatedly taking only top books can yield the order 1, 2, ..., N.
- Level
Medium5 of 10
- Topics
- Stack, Simulation, Implementation, Greedy
- Solved
- No attempts yet
Problem
After learning about stacks, Chanwoo fell in love with the data structures course.
Having resolved to devote himself to the data structures course alone, Chanwoo piled the textbooks of his other courses carelessly in a corner of his room as stacks. But as the midterm exam approached, he could no longer study only data structures, so he decided to tidy up the abandoned textbooks of his other courses and arrange them in numerical order.
The textbooks are numbered from to . Chanwoo can take only the textbook at the top of each stack, and since he must lay them out in the order he takes them, he has to take textbook , then , ..., then , then to arrange them in numerical order. Chanwoo says he will give up studying for the midterm if he cannot arrange the textbooks correctly. Write a program that tells him whether he can arrange them in numerical order.
Input
The first line gives the number of textbooks and the number of textbook stacks .
From the second line, lines give information about each stack.
For the -th stack, the first line gives the number of textbooks in the stack, and the second line gives integers separated by spaces.
Each integer is a textbook number, given starting from the textbook at the bottom.
Each integer from to appears exactly once as a textbook number.
Output
Print Yes if the textbooks can be taken in the correct order, and No otherwise.
Constraints
- The sum of all is always .