#C1125. Longest Path in a DAG
Longest Path in a DAG
Longest Path in a DAG
You are given a directed acyclic graph (DAG) with n nodes labeled from 1 to n and m directed edges. Your task is to compute the length of the longest path in the graph. A path is defined as a sequence of vertices where each consecutive pair is connected by a directed edge. The length of a path is the number of edges it contains. Formally, if the longest path has vertices \(v_1, v_2, \ldots, v_k\), then its length is \(k-1\).
Note: The input graph is guaranteed to be acyclic, so a topological ordering exists.
Input Constraints:
1 \(\leq n \leq\) (a reasonable upper limit, e.g., 105)
0 \(\leq m \leq\) (depends on n)
inputFormat
The first line of input contains two integers \(n\) and \(m\) representing the number of nodes and the number of edges, respectively. Each of the following \(m\) lines contains two integers \(u\) and \(v\) indicating a directed edge from node \(u\) to node \(v\>.
Input is provided via standard input (stdin).
outputFormat
Output a single integer representing the length of the longest path in the DAG. The result should be printed to standard output (stdout).
## sample5 6
1 2
1 3
3 4
3 5
4 5
2 4
3