Longest Shortest Paths
Time limit5sMemory limit1024 MB
Given a tree or a graph with exactly one cycle, find the longest shortest path length and how many shortest paths have that length.
- Level
Medium7 of 10
- Topics
- Tree, Graph, DFS, Dynamic programming
- Solved
- No attempts yet
Problem
Bajtazar learned what a graph is in his computer science class. A graph is a set of vertices and edges that connect pairs of vertices. A graph is connected if there is a path between any two vertices, which may pass through other vertices. A cycle is a sequence of vertices such that each pair of consecutive vertices and is joined by an edge, and so are the first and last vertices and .
While reading web pages, Bajtazar wanted to experiment with two special kinds of graphs: trees and jellyfish.
A graph is a tree if it is connected and has no cycles. A jellyfish is made from a tree by adding exactly one edge between two different vertices that are not yet joined by an edge. So a jellyfish always has exactly one cycle.
The shortest path between two vertices is the shortest route between them along edges. Formally, it is a sequence of vertices with all vertices distinct, where each pair of consecutive vertices is joined by an edge. The figure below shows the shortest paths between vertices 1 and 6 in red. In the jellyfish, the shortest path does not go through vertex 4, since such a route would not be the shortest.
Bajtazar wants to know the length of the longest of the shortest paths in his graph, and the number of ways to choose such a path. Two paths are different if they differ in at least one vertex. That vertex does not have to be an endpoint.
Input
The first line contains two natural numbers () and , separated by a single space. They are the number of vertices and the number of edges. is always equal to (jellyfish) or (tree).
Each of the following lines describes one edge with two natural numbers and (, ), the vertices it joins. No edge is repeated.
Output
Print exactly two lines. The first line contains the length of the longest shortest path in the input graph. The second line contains the number of different such paths.



