Dr. Anne Anderson has different courses, numbered
to
. Some of those courses have prerequisites, defined by
prerequisite pairs. For each pair, course
is a prerequisite of course
, meaning that course
must be completed in a semester strictly before course
. A course can be a prerequisite for multiple courses, and a course can have multiple different prerequisites.
Ratish had his heart set on taking specific courses, and he's willing to take any number of courses per semester. However, he wants to follow a few rules with his schedule:
He wants to finish all of his requested courses in as few semesters as possible.
He doesn't want to take any courses that he doesn't need to.
He wants to take each of the courses that he must take in the earliest semester possible.
Given his requirements, Ratish asks you to create a high school plan for him. He wants you to tell him the minimum number of semesters he needs to be in school, . He also wants you to tell him the courses he should take in each semester to finish high school in
semesters. To make it easier for him, he also wants you to sort the courses he should take in each semester. Can you write a program to help him?
Constraints
No course is a direct or indirect prerequisite of itself.
There are no duplicate prerequisite pairs.
Subtask 1 [25%]
Each course has one or less prerequisites, and is the prerequisite of one or less courses.
Subtask 2 [35%]
Subtask 3 [40%]
No additional constraints.
Input Specification
The first line contains the integer .
The second line contains the integer .
The third line contains the integer .
The fourth line contains integers separated by spaces, the courses that Ratish wants to take.
The next lines contain the integers
and
, indicating that course
is a prerequisite of course
.
Output Specification
The first line contains the integer .
For the following lines, line
contains all of the courses taken in semester
, separated by spaces.
Sample Input 1
6
4
2
1 2
5 4
3 1
3 2
4 2
Sample Output 1
3
3 5
1 4
2
Explanation For Sample 1
Ratish wants to take courses 1 and 2. The most optimal way to do this would be to take courses 3 and 5 in semester 1, courses 1 and 4 in semester 2, and course 2 in semester 3.
Ratish does not need to take course 6 in order to take his desired courses.
Sample Input 2
9
9
3
2 1 3
4 6
3 5
6 5
6 7
5 1
7 1
6 2
1 8
5 9
Sample Output 2
4
3 4
6
2 5 7
1
Explanation for Sample 2
Course 2 can be completed in either semester 3 or 4 to complete high school in 4 semesters. However, Ratish wants to complete each of his required courses as soon as possible, so course 2 must be completed in semester 3.
Comments