eolymp
bolt
Try our new interface for solving problems
Problems

Two Longest Paths

Two Longest Paths

Tekuhp is a tourist city. There are \textbf{N} intersections in the city, connected with \textbf{M} one-way roads. Each one-way road connects from some intersection to another. There maybe many roads that connect a pair of intersections. To make the city very amazing, the roads are constructed so that it is \textbf{not possible} to start at some intersection, travel along the roads, and return to the starting intersection. (It remains a strange secret how people of Tekuhp return home from work each day.) There are two groups of tourists planning to visit the city. They want to travel along the roads from some intersection to another. However, both groups do not want to run into each other. So they want two paths \textbf{P_1} and \textbf{P_2}, each \textbf{P_i}, for \textbf{1}≤ \textbf{i} ≤ \textbf{2}, starts at some intersection \textbf{s_i} and ends at intersection \textbf{t_i}, such that both paths share \textbf{no intersections}, including the starting and the ending intersections. However, it is possible that a path \textbf{P_i} may contain only one node, i. e., \textbf{s_i = t_i}. Tourists also want to visit many places. Since you are a good planner, you want to maximize the total number of intersections in both paths. \InputFile First line of the input contains an integer \textbf{T} (\textbf{1} ≤ \textbf{T} ≤ \textbf{10}), the number of test cases. After that \textbf{T} test cases follow. Each test case starts with integers \textbf{N} and \textbf{M} (\textbf{1} ≤ \textbf{N} ≤ \textbf{300}; \textbf{1} ≤ \textbf{M} ≤ \textbf{3000}), where \textbf{N} denotes the number of intersections and \textbf{M} denotes the number of roads. The intersections are numbered from \textbf{1} to \textbf{N}. After that \textbf{M} lines, describing road connection, follow. Each line contains two integers \textbf{A} and \textbf{B} denoting that there is a one-way road from intersection \textbf{A} to intersection \textbf{B}. \OutputFile The output must contain \textbf{T} lines, each line for each test case. For each test case, the output contains an integer \textbf{L}denoting the maximum number of intersections in two nonintersecting paths.
Time limit 1 second
Memory limit 64 MiB
Input example #1
3
3 2
1 2
2 3
8 9
1 2
2 3
3 4
5 6
6 7
7 8
2 6
6 3
3 7
4 2
1 2
3 4
Output example #1
3
8
4
Source ACM-ICPC Thailand National Programming Contest 2010, Prince of Songkla University Phuket Campus 24 August 2010