Distance Sum
Time limit4sMemory limit512 MB
Given a connected undirected unweighted graph with at most n+42 edges, compute the sum of shortest-path distances over all unordered vertex pairs.
- Level
Hard8 of 10
- Topics
- Graph, BFS, Tree, Implementation
- Solved
- No attempts yet
Problem
You are given a connected undirected unweighted graph. The distance between two vertices and is the number of edges in the shortest path between them. Find the sum of over all unordered pairs of vertices .
Input
The first line contains two integers and ( ; ), the number of vertices and the number of edges. The vertices are numbered from to .
Each of the following lines contains two integers and (; ), the endpoints of the -th edge.
There is at most one edge between every pair of vertices.
Output
Output a single integer: the sum of the distances between all unordered pairs of vertices in the graph.
Hint
In the first example, the distance between the four pairs of vertices connected by an edge is 1, and .