#P8028. Maximum Long Chain in a DAG

    ID: 21212 Type: Default 1000ms 256MiB

Maximum Long Chain in a DAG

Maximum Long Chain in a DAG

A microbiologist has (n) blue-green algae bacteria. There are (m) given food chains among these bacteria, where each food chain is represented by a pair ((a_i, b_i)) indicating that there is a food chain between bacteria (a_i) and (b_i). A sequence of food chains that are connected consecutively forms a long chain, and the length of a long chain is defined as the number of bacteria on that chain.

You are allowed to add extra food chains between pairs of bacteria arbitrarily, with the only restriction that after adding, the resulting graph (consisting of all the original and added food chains) must not contain any cycle.

Determine the maximum possible length of a long chain after you perform an arbitrary number of such additions.

(\textbf{Note:}) Since you are allowed to add edges, you can always arrange the bacteria in a linear order that respects the given constraints and then add edges between consecutive bacteria. Hence, the answer will be (n).

inputFormat

The input consists of:\

  • One line containing two integers (n) and (m) (the number of bacteria and the number of given food chains).
  • Then follow (m) lines, each containing two integers (a_i) and (b_i), indicating there is a food chain between bacteria (a_i) and (b_i). You may assume the given food chains do not form a cycle.

outputFormat

Output a single integer representing the maximum length of a long chain after performing the optimal additions.

sample

4 2
1 2
3 4
4