#C12444. Course Completion

    ID: 41872 Type: Default 1000ms 256MiB

Course Completion

Course Completion

You are given a number of courses and a list of prerequisite pairs. Each prerequisite pair (a, b) indicates that you must take course b before course a. Determine whether it is possible to finish all courses. This problem can be modeled as detecting a cycle in a directed graph. If there is a cycle, it is impossible to take all courses; otherwise, it is possible.

More formally, let \(n\) be the total number of courses. You are given an integer \(m\) indicating the number of prerequisite pairs. Then, \(m\) lines follow, where each line contains two integers \(a\) and \(b\), meaning that course \(b\) is a prerequisite for course \(a\). Your task is to output true if it is possible to finish all courses, and false otherwise.

Note: Use topological sorting or cycle detection algorithms to solve this problem.

inputFormat

The input is read from standard input and has the following format:

n
m
course1 prerequisite1
course2 prerequisite2
... (m lines)

Where:

  • n is an integer representing the total number of courses.
  • m is an integer representing the number of prerequisite pairs.
  • Each of the next m lines contains two space-separated integers representing a prerequisite relationship.

outputFormat

The output should be written to standard output and consist of a single line:

result

Where result is true if it is possible to complete all courses, and false otherwise.

## sample
3
0
true