#P1989. 【模板】无向图三元环计数
【模板】无向图三元环计数
Background
A triangle in an undirected graph refers to a subgraph of such that has exactly three vertices , and exactly three edges $\langle u, v \rangle, \langle v, w \rangle, \langle w, u \rangle$. Two triangles are different if and only if there exists a vertex such that and .
Problem Description
Given a simple undirected graph with vertices and 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 and the number of edges .
Lines to each contain two integers separated by a space, indicating an edge connecting vertex and vertex .
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 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): , .
- Subtask 2 (70 points): No special properties.
For of the data, , , . 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