#P1989. 【模板】无向图三元环计数

【模板】无向图三元环计数

Description

Given a simple undirected graph with nn vertices and mm edges, find the number of triangles in it.

Input Format

Each test point has one and only one set of testdata.

The first line contains two integers separated by a space, representing the number of vertices nn and the number of edges mm.

Lines 22 to (m+1)(m + 1) each contain two integers u,vu, v separated by a space, indicating an edge connecting vertex uu and vertex vv.

Output Format

Output one line with one integer, representing the number of triangles in the graph.

3 3
1 2
2 3
3 1

1
5 8
1 2
2 3
3 5
5 4
4 2
5 2
1 4
3 4

5

Hint

[Sample 2 Explanation]

There are 55 triangles. The vertices contained in each triangle are $\{1, 2, 4\}, \{2, 3, 4\}, \{2, 3, 5\}, \{2, 4, 5\}, \{3, 4, 5\}$.

[Constraints]

This problem uses bundled multi-testpoint evaluation and has two subtasks.

  • Subtask 1 (30 points): n500n \le 500, m103m \le {10}^3.
  • Subtask 2 (70 points): No special properties.

For 100%100\% of the data, 1n1051 \le n \le 10^5, 1m2×1051 \le m \le 2 \times {10}^5, 1u,vn1 \le u, v \le n. The given graph has no multiple edges or self-loops, but it is not guaranteed to be connected.

[Note]

  • Please pay attention to the impact of constant factors on program efficiency.

Translated by ChatGPT 5