Interview Questions/Coding/Simple Cycle Audit

Simple Cycle Audit

Preview mode. Log in to edit, run, submit, and save progress.

Hard

Simple Cycle Audit

A compliance tool receives small undirected network snapshots. A cycle is counted only when it is simple: no repeated vertices and no repeated edges. For each network, report how many distinct simple cycles it contains. The same cycle must not be counted again just because it is started from another vertex or traversed in the opposite direction. Input Format: The first line contains t. Each case starts with n and m. The following m lines contain undirected edges u v. Output Format: For each case, print the number of distinct simple cycles.

Examples

Example 1
Input:
2
4 6
1 2
1 3
1 4
2 3
2 4
3 4
4 4
1 2
2 3
3 4
4 1
Output:
7

Explanation: The complete graph on four vertices contains four triangles and three 4-cycles. The square case contains one simple cycle.

Approach hint

Pick one canonical smallest vertex for a cycle.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp