#P8028. [COCI 2021/2022 #3] Cijanobakterije

[COCI 2021/2022 #3] Cijanobakterije

Description

A microbiologist has nn cyanobacteria. Among these bacteria, there are mm pairs (ai,bi)(a_i, b_i), meaning there is a biological chain between aia_i and bib_i. Several biological chains can be connected one after another to form a long chain. The length of a long chain is defined as the number of bacteria on that long chain.

Now you may add some biological chains between pairs of bacteria, such that after adding, the entire set of biological chains contains no cycles.

After performing some add-chain operations, find the maximum possible length of the longest long chain.

Input Format

The first line contains two integers n,mn, m.

The next mm lines each contain two positive integers ai,bia_i, b_i, indicating that there is a biological chain between bacteria aia_i and bib_i. The data guarantees that aibia_i \neq b_i, and the same biological chain appears at most once. Also, these mm biological chains contain no cycles.

Output Format

Output the length of the longest long chain.

100 0
100
8 6
1 2
1 3
1 4
5 6
5 7
5 8
6
6 5
1 2
2 3
3 4
4 6
4 5
5

Hint

[Sample 2 Explanation]

After adding a biological chain between 22 and 66, the longest long chain is 3126573-1-2-6-5-7, with length 66.

[Constraints and Notes]

This problem uses bundled subtasks.

  • Subtask 1 (15 pts): m=n1m = n - 1.
  • Subtask 2 (6 pts): bi=ai+1b_i = a_i + 1.
  • Subtask 3 (6 pts): 1ai21 \le a_i \le 2.
  • Subtask 4 (15 pts): 1n10001 \le n \le 1000.
  • Subtask 5 (28 pts): No special restrictions.

For 100%100\% of the testdata, 1n1051 \le n \le 10^5, 0m<n0 \le m \lt n, 1ai,bin1 \le a_i, b_i \le n.

[Hints and Explanation]

Translated from COCI 2021-2022 CONTEST #3 Task 2 Cijanobakterije.

The score of this problem follows the original COCI problem, with a full score of 7070.

Translated by ChatGPT 5