cswork / OAOpen practice workspace

CommvaultOA #3

Number of Good Pairs

Estimated MediumJudge-ready

Topics (AI-labelled)Union-Find

In an undirected graph, count the unordered node pairs whose endpoints belong to different connected components.

This site's standard input/output format and supplementary bounds follow this problem's description.

Input

The input is N M (1≤N≤65,535, 0≤M≤2×10^5), followed by M lines of edges u v (1-based, 1≤u,v≤N). Duplicate edges and self-loops are allowed, and the result fits safely in a signed 32-bit integer.

Output

Output the good pair count (as a 64-bit integer).

Example 1

Input:

5 3
1 2
2 3
4 5

Output:

6

Example 2

Input:

4 0

Output:

6

Example 3

Input:

1 2
1 1
1 1

Output:

0

Editorial

Loading solution…