Sum of Distances in Cactus
Time limit1sMemory limit256 MB
Given a connected cactus graph with n vertices and m edges, compute the sum of shortest-path distances over all unordered vertex pairs.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Graph, DFS
- Solved
- No attempts yet
Problem
Find the sum of the distances between all pairs of vertices in a cactus graph. A cactus graph is a graph in which every edge belongs to at most one simple cycle. The distance between two vertices is the number of edges on the shortest path connecting them.
Input
The first line contains two integers and , the number of vertices and the number of edges in the cactus.
Each of the following lines contains two integers and , the labels of the vertices joined by an edge.
Output
Print a single line containing the sum of the distances between all pairs of vertices.
Constraints
The graph is connected and has no self-loops or multiple edges.