Call It What You Want
Time limit1sMemory limit64 MB
Given a connected graph with at most four edges beyond a spanning tree, find the length of the longest simple path.
- Level
Medium7 of 10
- Topics
- Graph, Tree, Dynamic programming
- Solved
- No attempts yet
Problem
Professor Zhang has heard that the longest path problem cannot be solved in polynomial time for arbitrary graphs unless P = NP. Now, Professor Zhang would like to solve this problem in polynomial time in some graphs.
The longest path problem is the problem of finding a simple path of maximum length in a given graph. A path is called simple if it does not have any repeated vertices. The length of a path is the number of edges in this path.
Input
There are multiple test cases. The first line of input contains an integer (about ) indicating the number of test cases. For each test case:
The first line contains two integers and (, ): the number of vertices and the number of edges.
Each of the following lines contains two integers and which denotes an edge between vertices and (, ).
It is guaranteed that the graph is connected and does not contain multiple edges.
The total size of the input is at most MiB.
Output
For each test case, output an integer denoting the length of the longest path.