summaryrefslogtreecommitdiff
path: root/test/graph/reroot.cpp
blob: c7c46087d50fc0b82633fda52f58b954cbe98047 (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
#include "../util.h"
#include <graph/reroot.cpp>

void stress_test() {
	for (int tries = 0; tries < 50'000; tries++) {
		int n = Random::integer<int>(1, 50);
		Graph<NoData> g(n);
		g.tree();

        adj.assign(n, {});
		g.forEdges([&](int a, int b){
			adj[a].emplace_back(b, Random::integer(0, 10));
			adj[b].emplace_back(a, Random::integer(0, 10));
		});

        Reroot re;
        auto ans = re.solve();

        auto dfs = [&](auto& self, int u, int p) -> ll {
            ll val = 0;
            for (auto [v, w] : adj[u]) if (v != p) {
                val ^= ((v ^ u) + self(self, v, u)) * w % MOD;
            }
            return u ^ val;
        };
        for (int i=0; i<n; i++) {
            if (ans[i] != dfs(dfs, i, -1)) {
                cerr << "WA expected " << dfs(dfs, i, -1) << " got " << ans[i] << FAIL;
            }
        }
	}
	cerr << "tested random 50'000 tests" << endl;
}

constexpr int N = 1'000'000;
void performance_test() {
	timer t;
	Graph<NoData> g(N);
	g.tree();
    adj.assign(N, {});
	g.forEdges([&](int a, int b){
        adj[a].emplace_back(b, Random::integer(0, (int)1e9));
        adj[b].emplace_back(a, Random::integer(0, (int)1e9));
	});

    ll hash = 0;
	t.start();
    Reroot re;
    auto ans = re.solve();
    hash = accumulate(begin(ans), end(ans), 0LL);
	t.stop();
	if (t.time > 1000) cerr << "too slow: " << t.time << FAIL;
	cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
}

int main() {
	stress_test();
	if (!sanitize) performance_test();
}