#P1989. Count the Number of Triangles in an Undirected Graph

    ID: 15271 Type: Default 1000ms 256MiB

Count the Number of Triangles in an Undirected Graph

Count the Number of Triangles in an Undirected Graph

You are given a simple undirected graph with n vertices and m edges. The graph is provided in the following format:

$$n \quad m$$

followed by m lines, each containing two integers $$u$$ and $$v$$, representing an edge between vertices $$u$$ and $$v$$. It is guaranteed that the graph does not have multiple edges or self-loops.

Your task is to count the number of triangles (i.e. 3-cycles) in the graph. A triangle is a cycle of length 3 where any two distinct vertices in the cycle are connected by an edge.

inputFormat

The first line contains two space-separated integers $$n$$ and $$m$$.

The next $$m$$ lines each contain two space-separated integers $$u$$ and $$v$$, representing an undirected edge between vertices $$u$$ and $$v$$.

outputFormat

Output a single integer which is the number of triangles in the graph.

sample

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