summaryrefslogtreecommitdiff
path: root/test/graph/articulationPoints.bridges.cpp
blob: a1b89d29b8b3bfd0c20503bc7f7ba9c3fee2b88a (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
#include "../util.h"
struct edge {
	ll from, to, id;
};
#define Edge edge
#include <graph/articulationPoints.cpp>
#undef Edge

vector<bool> naiveBridges(const vector<pair<int, int>>& edges) {
	vector<bool> res(sz(edges));

	vector<int> seen(sz(adj), -1);
	for (int i = 0; i < sz(edges); i++) {
		auto [a, b] = edges[i];
		vector<int> todo = {a};
		seen[a] = i;
		while (!todo.empty() && seen[b] != i) {
			int c = todo.back();
			todo.pop_back();
			for (auto e : adj[c]) {
				if (e.id == i) continue;
				if (seen[e.to] == i) continue;
				seen[e.to] = i;
				todo.push_back(e.to);
			}
		}
		res[i] = seen[b] != i;
	}
	return res;
}

void stress_test_bridges() {
	ll queries = 0;
	for (int tries = 0; tries < 200'000; tries++) {
		int n = Random::integer<int>(1, 30);
		int m = Random::integer<int>(0, max<int>(1, min<int>(300, n*(n-1) / 2 + 1)));
		Graph<NoData, 0, 1> g(n);
		g.erdosRenyi(m);

		adj.assign(n, {});
		vector<pair<int, int>> edges;
		g.forEdges([&](int a, int b){
			adj[a].push_back({a, b, sz(edges)});
			adj[b].push_back({b, a, sz(edges)});
			edges.emplace_back(a, b);
		});

		auto expected = naiveBridges(edges);
		find();
		vector<bool> got(sz(edges));
		for (auto e : bridges) {
			if (got[e.id]) cerr << "error: duclicate" << FAIL;
			got[e.id] = true;
		}

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

int main() {
	stress_test_bridges();
}