Dr. Anne Anderson Coding Contest 1 P6 - Designing An Academic Course Calendar

View as PDF

Submit solution


Points: 10 (partial)
Time limit: 1.0s
Memory limit: 256M

Author:
Problem type

Dr. Anne Anderson has N different courses, numbered 1 to N. Some of those courses have prerequisites, defined by M prerequisite pairs. For each pair, course A_i is a prerequisite of course B_i, meaning that course A_i must be completed in a semester strictly before course B_i. A course can be a prerequisite for multiple courses, and a course can have multiple different prerequisites.

Ratish had his heart set on taking K 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, X. He also wants you to tell him the courses he should take in each semester to finish high school in X 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

1 \leq K \leq N \leq 2 \times 10^5

0 \leq M \leq 2 \times 10^5

1 \leq A_i, B_i \leq N

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%]

K = N

Subtask 3 [40%]

No additional constraints.

Input Specification

The first line contains the integer N.

The second line contains the integer M.

The third line contains the integer K.

The fourth line contains K integers separated by spaces, the courses that Ratish wants to take.

The next M lines contain the integers A_i and B_i, indicating that course A_i is a prerequisite of course B_i.

Output Specification

The first line contains the integer X.

For the following X lines, line i contains all of the courses taken in semester i, 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

There are no comments at the moment.