题目:
You are given a program you want to execute as a set of tasks organized in a dependency graph. The dependency graph is a directed acyclic graph: each task can depend on results of one or several other tasks, and there are no directed circular dependencies between tasks. A task can only be executed if all tasks it depends on have already completed.
Some of the tasks in the graph can only be executed on a coprocessor, and the rest can only be executed on the main processor. In one coprocessor call you can send it a set of tasks which can only be executed on it. For each task of the set, all tasks on which it depends must be either already completed or be included in the set. The main processor starts the program execution and gets the results of tasks executed on the coprocessor automatically.
Find the minimal number of coprocessor calls which are necessary to execute the given program.
Input
The first line contains two space-separated integers N (1 ≤ N ≤ $10^5$) — the total number of tasks given, and M (0 ≤ M ≤ $10^5$) — the total number of dependencies between tasks.
The next line contains N space-separated integers . If $E_i$= 0, task i can only be executed on the main processor, otherwise it can only be executed on the coprocessor.
The next M lines describe the dependencies between tasks. Each line contains two space-separated integers T1 and T2 and means that task T1 depends on task T2 (T1 ≠ T2). Tasks are indexed from 0 to N - 1. All M pairs (T1, T2) are distinct. It is guaranteed that there are no circular dependencies between tasks.
Output
Output one line containing an integer — the minimal number of coprocessor calls necessary to execute the program.
Examples
input
1 | 4 3 |
output
1 | 2 |
input
1 | 4 3 |
output
1 | 1 |
思路:
其实思路还是比较好想的,肯定是要从入度为0的点入手.
solution 1
1 | 我们不难想到这样一个策略,从入度为0的点入手,如果当前处理的是0,那么res+1,否则一直继续. |
solution 2
1 | 然后依然WA3. |
solution
1 | 我们维护两个入读为0的队列分别存值为0的点和值为1的点,我们先把度为0的点的队列拿光后,再把度为1的点的队列中点拿光,这样一直做下去就可以了. |