#K371. Longest Employee Chain
Longest Employee Chain
Longest Employee Chain
You are given a directed graph representing a follow relationship among employees. The graph has N employees (numbered from 0 to N-1) and M directed edges. Each directed edge from employee a to employee b implies that employee a follows employee b directly.
Your task is to determine the length of the longest chain of employees where each one follows the next directly. In other words, if we define a chain as a sequence of employees \(v_0, v_1, \dots, v_k\) such that there is a directed edge from \(v_i\) to \(v_{i+1}\) for each \(0 \leq i < k\), you need to compute the maximum possible value of \(k+1\) among all chains, with the additional constraint that the answer is at least L.
The input guarantees that there are no cycles that can lead to infinite chains. Use appropriate algorithms (e.g., depth-first search with memoization) to compute the chain lengths efficiently.
inputFormat
The input is given via standard input (stdin) and consists of multiple lines:
- The first line contains two integers N and L, where N is the number of employees and L is the minimum chain length.
- The second line contains a single integer M, the number of directed edges.
- The following M lines each contain two integers a and b, representing a directed edge from employee a to employee b.
outputFormat
Output via standard output (stdout) a single integer: the length of the longest chain of employees. Note that if no chain is longer than L, output L.
## sample6 2
6
0 1
1 2
2 3
3 4
4 5
1 3
6