#P1476. Minimal Rounds in 'Initial Dream' Game

    ID: 14762 Type: Default 1000ms 256MiB

Minimal Rounds in 'Initial Dream' Game

Minimal Rounds in 'Initial Dream' Game

The game "Initial Dream" tells the story of a determined young hero named pass battling the legendary demon king chinesesonic through various time dimensions. The game is built with a complex story flow featuring multiple branches where some branches can be pursued concurrently, while others require the completion of specific prior storylines.

To experience the full narrative, the player must complete all story branches. In this challenge, you are given a set of storylines along with prerequisites (dependencies) between them. Each storyline takes exactly 1 unit of time to complete, and any number of storylines without pending prerequisites can be completed simultaneously. Your task is to determine the minimum number of time units (rounds) needed to complete all the storylines.

The dependencies are provided as directed edges in a graph. Formally, if there is an edge from storyline u to storyline v, then storyline v can only be started after storyline u is finished. This can be modeled as a directed acyclic graph (DAG). The answer is the length of the longest path in the DAG, which can also be computed using a topological ordering approach.

In mathematical terms, if we denote the minimum round for node v as dp[v], and for every dependency edge \( u \to v \), then:

[ dp[v] = \max (dp[v], dp[u]+1)]

Your program should output the minimum number of rounds required to finish all the storylines.

inputFormat

The first line contains two integers n and m separated by a space, representing the number of storylines and the number of dependency relations, respectively.

The next m lines each contain two integers u and v (1-indexed), denoting that storyline v can only be started after the completion of storyline u.

You may assume that the graph is a directed acyclic graph (DAG) and that 1 \( \le n \le 10^5 \), 0 \( \le m \le 10^5 \).

outputFormat

Output a single integer, the minimum number of rounds needed to complete all the storylines.

sample

3 2
1 2
1 3
2