summaryrefslogtreecommitdiff
path: root/test/graph/euler.cpp
blob: 457ca994886e454d14f9142c23a063d1898b60b8 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "../util.h"
struct Euler {
	Euler(int n) : adj(n) {}
#include <graph/euler.cpp>
};

Euler eulerGraph(int n, int m) {
	Euler res(n);

	Graph<NoData> g(n);
	g.tree();
	g.forEdges([&](int a, int b) {
		res.addEdge(a, b);
	});

	for (int i = n-1; i < m; i++) {
		int a = Random::integer<int>(0, n);
		int b = Random::integer<int>(0, n);
		res.addEdge(a, b);
	}
	int last = -1;
	for (int i = 0; i < n; i++) {
		if (sz(res.adj[i]) % 2 != 0) {
			if (last >= 0) {
				res.addEdge(last, i);
				last = -1;
			} else {
				last = i;
			}
		}
	}
	if (last >= 0) cerr << "FAIL" << FAIL;

	return res;
}

void stress_test() {
	ll queries = 0;
	for (int tries = 0; tries < 200'000; tries++) {
		int n = Random::integer<int>(1, 30);
		int m = Random::integer<int>(n-1, 200);

		auto g = eulerGraph(n, m);
		
		vector<vector<int>> expected(n);
		for (int i = 0; i < n; i++) {
			for (auto [j, rev] : g.adj[i]) {
				expected[i].push_back(j);
			}
			sort(all(expected[i]));
		}

		g.euler(0);
		vector<vector<int>> got(n);
		if (g.cycle.front() != g.cycle.back()) cerr << "error: not cyclic" << FAIL;
		for (int i = 1; i < sz(g.cycle); i++) {
			int a = g.cycle[i-1];
			int b = g.cycle[i];
			got[a].push_back(b);
			got[b].push_back(a);
		}
		for (auto& v : got) sort(all(v));

		if (got != expected) cerr << "error" << FAIL;
		queries += n;
	}
	cerr << "tested random queries: " << queries << endl;
}

constexpr int N = 100'000;
constexpr int M = 1'000'000;
void performance_test() {
	timer t;
	auto g = eulerGraph(N, M);
	t.start();
	g.euler(0);
	t.stop();
	hash_t hash = 0;
	for (int x : g.cycle) hash += x;
	if (t.time > 500) cerr << "too slow: " << t.time << FAIL;
	cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
}

int main() {
	stress_test();
	performance_test();
}