summaryrefslogtreecommitdiff
path: root/graph/cycleCounting.cpp
blob: b64b230b4928c945900ebc5b81d9910efbfcb972 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
constexpr ll maxEdges = 128;
using cycle = bitset<maxEdges>;
struct cylces {
	ll n;
	vector<vector<pair<ll, ll>>> adj;
	vector<bool> seen;
	vector<cycle> paths, base;
	vector<pair<ll, ll>> edges;

	cylces(ll n) : n(n), adj(n), seen(n), paths(n) {}

	void addEdge(ll a, ll b) {
		adj[a].push_back({b, sz(edges)});
		adj[b].push_back({a, sz(edges)});
		edges.push_back({a, b});
	}

	void addBase(cycle cur) {
		for (cycle o : base) {
			o ^= cur;
			if (o._Find_first() > cur._Find_first()) cur = o;
		}
		if (cur.any()) base.push_back(cur);
	}

	void findBase(ll c = 0, ll p = -1, cycle cur = {}) {
		if (n == 0) return;
		if (seen[c]) {
			addBase(cur ^ paths[c]);
		} else {
			seen[c] = true;
			paths[c] = cur;
			for (auto e : adj[c]) {
				if (e.first == p) continue;
				cur[e.second].flip();
				findBase(e.first, c, cur);
				cur[e.second].flip();
	}}}

	//cycle must be constrcuted from base
	bool isCycle(cycle cur) {
		if (cur.none()) return false;
		init(n);
		for (ll i = 0; i < sz(edges); i++) {
			if (cur[i]) {
				cur[i] = false;
				if (findSet(edges[i].first) == 
				    findSet(edges[i].second)) break;
				unionSets(edges[i].first, edges[i].second);
		}}
		return cur.none();
	};

	ll count() {
		findBase();
		ll res = 0;
		for (ll i = 1; i < (1ll << sz(base)); i++) {
			cycle cur;
			for (ll j = 0; j < sz(base); j++) {
				if (((i >> j) & 1) != 0) cur ^= base[j];
			if (isCycle(cur)) res++;
		}
		return res;
	}
};