#P6680. [CCO 2019] Marshmallow Molecules

[CCO 2019] Marshmallow Molecules

Description

There is an undirected graph with NN vertices and MM edges. The graph has no multiple edges and no self-loops.

If a<b<ca<b<c and there is an edge between aa and bb, and there is also an edge between aa and cc, then an edge will be added between bb and cc.

Find the final number of edges.

Input Format

The first line contains two integers NN and MM.

The next MM lines each contain two integers aia_i and bib_i, indicating that there is an edge connecting aia_i and bib_i.

Output Format

Only one line with one integer, representing the final number of edges.

6 4
1 2
1 4
4 6
4 5
6
7 6
2 3
2 6
2 7
1 3
1 4
1 5

16

Hint

Explanation for Sample 1

You need to add two edges, (2,4)(2,4) and (5,6)(5,6).

Constraints and Limits

For 100%100\% of the testdata, it is guaranteed that 1N,M1051\le N,M\le 10^5, 1ai<biN1\le a_i<b_i\le N.

Subtask NN\le Special Constraints Score
1 100100 None 2020
2 5×1035\times 10^3
3 No special limit For every 1jN1\le j\le N, there is at least one pair (ai,bi)(a_i,b_i) with bi=jb_i=j
4 None 4040

Notes

This problem is translated from Canadian Computing Olympiad 2019 Day 2 T2 Marshmallow Molecules。

Translated by ChatGPT 5