Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], skipTags: ["script","noscript","style","textarea","code"], processEscapes: true }});

Problem K
Counting Cycles

Given an undirected graph, count the number of simple cycles in the graph. Here, a simple cycle is a connected subgraph all of whose vertices have degree exactly two.

Input

The input consists of a single test case of the following format.

$n$ $m$
$u_1$ $v_1$
...
$u_m$ $v_m$

A test case represents an undirected graph $G$.

The first line shows the number of vertices $n$ ($3 \leq n \leq 100 000$) and the number of edges $m$ ($n - 1 \leq m \leq n + 15$). The vertices of the graph are numbered from $1$ to $n$.

The edges of the graph are specified in the following $m$ lines. Two integers $u_i$ and $v_i$ in the
$i$-th line of these m lines mean that there is an edge between vertices $u_i$ and $v_i$. Here, you can assume that $u_i < v_i$ and thus there are no self loops.

For all pairs of $i$ and $j$ ($i \ne j$), either $u_i \ne u_j$ or $v_i \ne v_j$ holds. In other words, there are no parallel edges.

You can assume that $G$ is connected.

Output

The output should be a line containing a single number that is the number of simple cycles in the graph.

Sample Input 1

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

Sample Output 1

3

Sample Input 2

7 9
1 2
1 3
2 4
2 5
3 6
3 7
2 3
4 5
6 7

Sample Output 2

3

Sample Input 3

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

Sample Output 3

7

Solution in C++11 (judge verdict: Wrong Answer)

#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <cstring>
#include <algorithm>
#define xx first
#define yy second
#define mp make_pair
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;

const int maxn = 200005;

pii ori[maxn];

pii add[50]; int cntadd;

int father[maxn], sz[maxn];
int getfather(int x) { return father[x] == x ? x : father[x] = getfather(father[x]); }

int dep[maxn], pre[maxn];
vector<int> E[maxn];
vector<int> HAHA[maxn];
vector<int> Tmp;
int couldbe[maxn];
void dfs(int x, int fa) {
	dep[x] = dep[fa] + 1;
	pre[x] = fa;
	if (couldbe[x]) {
		HAHA[x] = Tmp;
		Tmp.push_back(x);
	}
	for (auto &y : E[x]) if (y != fa) {
		dfs(y, x);
	}
	if (couldbe[x]) {
		Tmp.pop_back();
	}
}

int num[maxn << 1], cntnum;
int deg[maxn];

int Sta[maxn], top;

bool cmp(int x, int y) {
	return dep[x] > dep[y];
}

int main() {
	//freopen("in", "r", stdin);
	int n, m; scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &ori[i].xx, &ori[i].yy);
	}

	for (int i = 1; i <= n; i++) father[i] = i;
	for (int i = 1; i <= m; i++) {
		int x = ori[i].xx, y = ori[i].yy;
		if (getfather(x) != getfather(y)) {
			father[getfather(x)] = getfather(y);
			E[x].push_back(y);
			E[y].push_back(x);
		}
		else {
			add[cntadd++] = ori[i];
			couldbe[ori[i].xx] = 1;
			couldbe[ori[i].yy] = 1;
			num[++cntnum] = ori[i].xx;
			num[++cntnum] = ori[i].yy;
		}
	}
	dfs(1, 0);

	sort(num + 1, num + 1 + cntnum); cntnum = unique(num + 1, num + 1 + cntnum) - num - 1;

	ll ans = 1;
	for (int i = 1; i < cntadd; i++) {
		for (int sta = 0; sta < 1 << i; sta++) {
			for (int x = 0; x <= cntnum; x++) deg[num[x]] = 0;
			for (int x = 0; x < i; x++) {
				if (sta >> x & 1) {
					deg[add[x].xx]++;
					deg[add[x].yy]++;
				}
			}
			deg[add[i].xx]++;
			deg[add[i].yy]++;
			bool flag = 0;
			for (int x = 1; x <= cntnum; x++) if (deg[num[x]] > 2) {
				flag = 1;
				break;
			}
			if (flag) continue;

			top = 0;
			for (int x = 1; x <= cntnum; x++) if (deg[num[x]] == 1) {
				Sta[++top] = num[x];
				sort(Sta + 1, Sta + 1 + top, cmp);
			}

			int CNT(0);
			for (int x = 1; x <= cntnum; x++) if (deg[num[x]]) {
				father[num[x]] = num[x];
				sz[num[x]] = 1;
				CNT++;
			}

			for (int x = 0; x < i; x++) {
				if (sta >> x & 1) {
					int fx = getfather(add[x].xx), fy = getfather(add[x].yy);
					sz[fy] += sz[father[fx]];
					father[fx] = fy;
				}
			}
			int fx = getfather(add[i].xx), fy = getfather(add[i].yy);
			sz[fy] += sz[father[fx]];
			father[fx] = fy;

			int FUCK(0);
			for (int o = 1; o <= top; o++) {
				int x = Sta[o];
				if (deg[x] != 1) continue;
				int N = HAHA[x].size();
				for (int k = N - 1; k >= 0; k--) {
					if (deg[HAHA[x][k]]) {
						if (deg[HAHA[x][k]] != 1) {
							flag = 1;
							break;
						}
						else {
							deg[HAHA[x][k]] = 2;
							deg[x] = 2;
						}
					}
				}
				if (deg[x] == 1) FUCK++;
			}

			if (flag) continue;

			if (FUCK == 0) ans++;
			else if (FUCK == 2) {
				if (sz[getfather(Sta[1])] == CNT) ans++;
			}

		}
	}
	printf("%lld\n", ans);
}