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 $E_{i} \in \{0,1 \}$. 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 T_1 and T_2 and means that task T_1 depends on task T_2 (T_1 ≠ T_2). Tasks are indexed from 0 to N - 1. All M pairs (T_1, T_2) 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
4 3
0 1 0 1
0 1
1 2
2 3

Output
2

Input
4 3
1 1 1 0
0 1
0 2
3 0

Output
1



-----Note-----

In the first test, tasks 1 and 3 can only be executed on the coprocessor. The dependency graph is linear, so the tasks must be executed in order 3 -> 2 -> 1 -> 0. You have to call coprocessor twice: first you call it for task 3, then you execute task 2 on the main processor, then you call it for for task 1, and finally you execute task 0 on the main processor.

In the second test, tasks 0, 1 and 2 can only be executed on the coprocessor. Tasks 1 and 2 have no dependencies, and task 0 depends on tasks 1 and 2, so all three tasks 0, 1 and 2 can be sent in one coprocessor call. After that task 3 is executed on the main processor.

Difficulty: HARD
Tags: Dynamic programming

Graph algorithms

Graph traversal

Greedy algorithms
Skill types: Dynamic programming

Greedy algorithms

Starter code:

Solutions:
from sys import stdin
from collections import deque
(n, m) = [int(x) for x in stdin.readline().split()]
e = [int(x) for x in stdin.readline().split()]
graph = [set() for x in range(n)]
reverse = [set() for x in range(n)]
root = set([x for x in range(n)])
for edge in range(m):
	(a, b) = [int(x) for x in stdin.readline().split()]
	graph[b].add(a)
	reverse[a].add(b)
	if a in root:
		root.remove(a)
done = set()
qMain = deque(list(root))
qCo = deque()
total = -1
while len(done) < n:
	total += 1
	while qCo:
		nxt = qCo.popleft()
		if e[nxt] == 0:
			qMain.append(nxt)
		else:
			done.add(nxt)
			for x in graph[nxt]:
				reverse[x].remove(nxt)
				if not reverse[x]:
					qCo.append(x)
	while qMain:
		nxt = qMain.popleft()
		if e[nxt] == 1:
			qCo.append(nxt)
		else:
			done.add(nxt)
			for x in graph[nxt]:
				reverse[x].remove(nxt)
				if not reverse[x]:
					qMain.append(x)
print(total)


from collections import deque
import sys
input = sys.stdin.readline

def put():
	return map(int, input().split())

def bfs_(main, co, flag):
	curr = main if flag else co
	while curr:
		x = curr.popleft()
		for y in graph[x]:
			inedge[y] -= 1
			if inedge[y] == 0:
				if is_co[y]:
					co.append(y)
				else:
					main.append(y)
	return (main, co)

def bfs():
	(main, co, ans) = (deque(), deque(), 0)
	for i in range(n):
		if inedge[i] == 0:
			if is_co[i]:
				co.append(i)
			else:
				main.append(i)
	while main or co:
		(main, co) = bfs_(main, co, True)
		if co:
			ans += 1
		(main, co) = bfs_(main, co, False)
	return ans
(n, m) = put()
is_co = list(put())
graph = [[] for i in range(n)]
(inedge, vis) = ([0] * n, [0] * n)
for _ in range(m):
	(x, y) = put()
	graph[y].append(x)
	inedge[x] += 1
print(bfs())


(n, m) = (int(x) for x in input().strip().split())
coproc = [int(x) for x in input().strip().split()]
edges = []
for _ in range(m):
	edges.append((int(x) for x in input().strip().split()))
inorder = [0] * n
adj = [[] for _ in range(n)]
for (u, v) in edges:
	adj[v].append(u)
	inorder[u] += 1
queue0 = [u for u in range(n) if inorder[u] == 0 and (not coproc[u])]
queue1 = [u for u in range(n) if inorder[u] == 0 and coproc[u]]
res = 0
while len(queue0) > 0 or len(queue1) > 0:
	while queue0:
		next0 = []
		for u in queue0:
			for v in adj[u]:
				inorder[v] -= 1
				if inorder[v] == 0:
					if coproc[v]:
						queue1.append(v)
					else:
						next0.append(v)
		queue0 = next0
	if queue1:
		res += 1
	while queue1:
		next1 = []
		for u in queue1:
			for v in adj[u]:
				inorder[v] -= 1
				if inorder[v] == 0:
					if coproc[v]:
						next1.append(v)
					else:
						queue0.append(v)
		queue1 = next1
print(res)


(n, m) = [int(x) for x in input().split()]
tps = {i: int(t) for (i, t) in enumerate(input().split())}
deps = {i: 0 for i in range(n)}
parents = {i: [] for i in range(n)}
leafs = {i for i in range(n)}
for i in range(m):
	(x, y) = [int(x) for x in input().split()]
	deps[x] += 1
	parents[y].append(x)
	if x in leafs:
		leafs.remove(x)
deep = {i: tps[i] for i in range(n)}
res = 0
while len(leafs) > 0:
	leaf = leafs.pop()
	if len(parents[leaf]) == 0:
		res = max(res, deep[leaf])
		continue
	for parent in parents[leaf]:
		l = deep[leaf]
		if tps[parent] == 1 and tps[leaf] == 0:
			l += 1
		if deep[parent] < l:
			deep[parent] = l
		deps[parent] -= 1
		if deps[parent] == 0:
			leafs.add(parent)
print(res)