#C5068. Task Completion Order

    ID: 48676 Type: Default 1000ms 256MiB

Task Completion Order

Task Completion Order

You are given n tasks and m dependency relations. Each dependency is represented as an ordered pair \( (a, b) \) indicating that task \( b \) depends on task \( a \), meaning task \( b \) can only be started after task \( a \) is completed.

Your goal is to determine whether it is possible to complete all tasks in an order that satisfies all dependency constraints. A valid ordering exists if and only if the dependency graph is a Directed Acyclic Graph (DAG). You may use Kahn's Algorithm to check for cycles in the graph.

Note: Although each task has an associated completion time, the times do not affect the ordering feasibility.

inputFormat

The input is given via standard input (stdin) and is formatted as follows:

  1. The first line contains two integers ( n ) and ( m ), where ( n ) is the number of tasks and ( m ) is the number of dependency relations.
  2. The second line contains ( n ) integers ( t_1, t_2, \ldots, t_n ) representing the time to complete each respective task.
  3. The following ( m ) lines each contain two integers ( a ) and ( b ), indicating that task ( b ) depends on task ( a ) (tasks are 1-indexed).

outputFormat

Output a single line to standard output (stdout) containing:

  • "Yes" if it is possible to complete all tasks by reordering them to satisfy the dependency constraints.
  • "No" if it is not possible (i.e. the dependency graph contains a cycle).## sample
5 0
3 2 1 4 5
Yes