Coprocessor
You are given a program composed of a set of tasks. These tasks are arranged in a directed acyclic graph (DAG) of dependencies: each task may rely on the results of one or more other tasks, and there are no circular dependencies among them. A task can only be executed once all the tasks it depends on have been completed.
Some tasks in the graph must be executed on a coprocessor, while others must be executed on the main processor. With a single call to the coprocessor, you can pass it a list of tasks that need to be executed there. For each task in this list, all its dependencies must have already been completed or be included in the same list. The main processor begins executing the program and automatically receives the results of task execution from the coprocessor.
Your goal is to determine the minimum number of coprocessor calls needed to execute the entire program.
Input
The first line contains two integers N and M (1 ≤ N, M ≤ 2·10^5
) – representing the number of tasks and the number of dependencies between them, respectively.
The second line contains N integers E[i]
(either 0 or 1), separated by spaces. If E[i]
= 0, task i must be executed on the main processor; if E[i]
= 1, it must be executed on the coprocessor.
The following M lines describe the dependencies between tasks. Each line contains two integers T[1]
and T[2]
, indicating that task T[1]
depends on task T[2]
(T[1]
≠ T[2]
). Tasks are numbered from 0 to N-1. All M pairs are unique.
Output
Print a single integer – the minimum number of coprocessor calls required to execute the program.