#C10946. Count Unique 4-Cycles
Count Unique 4-Cycles
Count Unique 4-Cycles
You are given a directed graph representing follow relationships among n nodes. Your task is to count the number of unique cycles of length 4. A cycle of length 4 is a sequence of four distinct vertices \(A, B, C, D\) such that there exist directed edges \(A \to B\), \(B \to C\), \(C \to D\) and \(D \to A\). Note that each cycle is considered unique regardless of its starting point, so each cycle may be counted multiple times if not handled properly. Your solution should output the count of such unique cycles.
Input Format: The first line contains two integers \(n\) and \(m\) denoting the number of nodes and the number of directed edges respectively. The following \(m\) lines each contain two integers \(u\) and \(v\) representing a directed edge from node \(u\) to node \(v\).
Output Format: Output a single integer, the number of unique 4-cycles in the graph.
Note: Each unique cycle is counted exactly once (even though the cyclic order might traverse it multiple times).
inputFormat
The input is read from stdin
and contains:
- 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 a directed edge from node \(u\) to node \(v\).
outputFormat
Output a single integer representing the number of unique 4-cycles in the graph. The output should be written to stdout
.
5 7
1 2
2 3
3 4
4 1
1 3
2 4
4 2
1