SDE Path

Course Schedule Problem

Medium

Course Schedule Problem

Given course prerequisites, determine whether it is possible to finish all courses.

Input format

The first line contains N (courses) and M (prerequisites). The next M lines contain pairs (b, a) meaning a is a prerequisite of b.

Output format

Print true if you can finish all courses (no cycle), false otherwise.

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
1 0
Expected output
true
Example 2
Input
12 28
1 3
3 10
11 1
10 8
9 6
4 10
4 8
8 2
6 4
0 10
3 6
2 8
2 7
9 2
6 5
1 11
0 9
7 10
9 5
0 5
9 11
8 3
2 3
8 9
3 5
11 4
11 7
5 4
Expected output
false