Problems > Longest Chain of Loop Clubs

Longest Chain of Loop Clubs

Difficulty: 2100 4000 ms 128 MB

dp graphs scc

A path may revisit nodes and edges. Print the maximum number of DISTINCT nodes visitable by a single (arbitrarily long) walk in the directed graph.

Input: the first line has N and M; each of the next M lines has a directed edge u → v.

Output: the maximum number of distinct nodes on one walk.

Constraints:

  • 1 ≤ N ≤ 104
  • 0 ≤ M ≤ 2×104

Sample Tests

Input 1
5 5
1 2
2 3
3 1
3 4
4 5
Output 1
5
Input 2
3 1
1 2
Output 2
2

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.