#P9794. [NERC 2018] Distance Sum

[NERC 2018] Distance Sum

Background

Translated from Problem D of NERC 2018.

Problem Description

You are given a connected undirected graph with nn vertices and mm edges. Define the distance d(u,v)d(u, v) between uu and vv as the number of edges on the shortest path from uu to vv.

Now, compute u=1nv=u+1nd(u,v)\sum_{u=1}^n \sum_{v=u+1}^n d(u,v).

Input Format

The first line contains two integers n(1n105)n(1 \leq n \leq 10^5) and m(n1mn+42)m(n - 1 \leq m \leq n + 42), representing the number of vertices and the number of edges.

The next mm lines each contain two integers xix_i and yi(1xi,yin,xiyi)y_i(1 \leq x_i,y_i \leq n, x_i \neq y_i), indicating that there is an edge between xix_i and yiy_i.

It is guaranteed that there are no multiple edges or self-loops.

Output Format

Output u=1nv=u+1nd(u,v)\sum_{u=1}^n \sum_{v=u+1}^n d(u,v).

4 4
1 2
2 3
3 1
3 4
8
7 10
1 2
2 6
5 3
5 4
5 7
3 6
1 7
5 1
7 4
4 1
34

Hint

For all testdata, it is guaranteed that 1n1051 \leq n \leq 10^5, n1mn+42n-1 \leq m \leq n + 42, 1xi,yin1 \leq x_i, y_i \leq n, and xiyix_i \neq y_i.

The graph of Sample 1 is:

In it, d(1,2)=1d(1,2) = 1, d(1,3)=1d(1,3) = 1, d(1,4)=2d(1,4) = 2, d(2,3)=1d(2,3) = 1, d(2,3)=2d(2,3) = 2, d(3,4)=1d(3,4) = 1, and the total sum is 1+1+2+1+2+1=81 + 1 + 2 + 1 + 2 + 1 = 8.

Sample 2 is:

Translated by ChatGPT 5