#abc282d. D - Make Bipartite 2

D - Make Bipartite 2

Score : 400400 points

问题描述

给定一个包含 NN 个顶点和 MM 条边的简单无向图 GG(简单图不包含自环或多重边)。对于 i=1,2,,Mi = 1, 2, \ldots, M,第 ii 条边连接顶点 uiu_i 和顶点 viv_i

请输出满足以下两个条件的整数对 (u,v)(u, v) 的数量,其中 1u<vN1 \leq u < v \leq N

  • GG 中不存在连接顶点 uu 和顶点 vv 的边。
  • 在图 GG 中添加一条连接顶点 uu 和顶点 vv 的边后,得到的图是一个二分图。

什么是二分图?

二分图 是一种无向图,其定义为:如果可以将每个顶点涂成黑色或白色,以满足以下条件,则称该图为二分图。

  • 没有边连接颜色相同的顶点。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a simple undirected graph GG with NN vertices and MM edges (a simple graph does not contain self-loops or multi-edges). For i=1,2,,Mi = 1, 2, \ldots, M, the ii-th edge connects vertex uiu_i and vertex viv_i.

Print the number of pairs of integers (u,v)(u, v) that satisfy 1u<vN1 \leq u \lt v \leq N and both of the following conditions.

  • The graph GG does not have an edge connecting vertex uu and vertex vv.
  • Adding an edge connecting vertex uu and vertex vv in the graph GG results in a bipartite graph.

What is a bipartite graph?

An undirected graph is said to be bipartite if and only if one can paint each vertex black or white to satisfy the following condition.

  • No edge connects vertices painted in the same color.

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • $0 \leq M \leq \min \lbrace 2 \times 10^5, N(N-1)/2 \rbrace$
  • 1ui,viN1 \leq u_i, v_i \leq N
  • The graph GG is simple.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN MM

u1u_1 v1v_1

u2u_2 v2v_2

\vdots

uMu_M vMv_M

Output

Print the answer.

Sample Input 1

5 4
4 2
3 1
5 2
3 2

Sample Output 1

2

We have two pairs of integers (u,v)(u, v) that satisfy the conditions in the problem statement: (1,4)(1, 4) and (1,5)(1, 5). Thus, you should print 22.
The other pairs do not satisfy the conditions. For instance, for (1,3)(1, 3), the graph GG has an edge connecting vertex 11 and vertex 33; for (4,5)(4, 5), adding an edge connecting vertex 44 and vertex 55 in the graph GG does not result in a bipartite graph.

Sample Input 2

4 3
3 1
3 2
1 2

Sample Output 2

0

Note that the given graph may not be bipartite or connected.

Sample Input 3

9 11
4 9
9 1
8 2
8 3
9 2
8 4
6 7
4 6
7 5
4 5
7 8

Sample Output 3

9

update @ 2024/3/10 11:50:13