45#ifndef ALEPH_TESTS_GRAPH_ORACLES_H
46#define ALEPH_TESTS_GRAPH_ORACLES_H
72 while (
parent[
size_t(x)] != x)
93 for (
const auto & a : s.
arcs)
94 d += (a[0] == x) + (a[1] == x);
104 for (
size_t i = 0; i < s.
arcs.size(); ++i)
106 const int u = s.
arcs[i][0], v = s.
arcs[i][1];
107 if (
int(i) == skip_arc or u == skip_node or v == skip_node)
111 std::vector<int> comp(
size_t(s.
n));
112 for (
int x = 0; x < s.
n; ++x)
113 comp[
size_t(x)] = uf.
find(x);
123 for (
int x = 0; x < s.
n; ++x)
125 roots.insert(comp[
size_t(x)]);
126 return int(roots.size());
134 for (
int x = 0; x < s.
n; ++x)
146 if (comp[
size_t(x)] == comp[size_t(start)])
156 for (
size_t i = 0; i < s.
arcs.size(); ++i)
166 for (
const auto & a : s.
arcs)
167 if (not uf.
join(a[0], a[1]))
175 for (
size_t i = 0; i < s.
arcs.size(); ++i)
177 const int u = s.
arcs[i][0], v = s.
arcs[i][1];
178 if (u != x
and v != x)
183 if (comp[
size_t(u)] == comp[
size_t(v)])
193 std::vector<int>
color(
size_t(s.
n), -1);
199 std::vector<int> stack = {
root};
200 while (not stack.empty())
202 const int x = stack.back();
204 for (
const auto & a : s.
arcs)
205 for (
int k = 0;
k < 2; ++
k)
206 if (a[
size_t(
k)] == x)
208 const int y = a[size_t(1 -
k)];
209 if (
color[
size_t(
y)] == -1)
226 const size_t m = s.
arcs.size();
228 for (
unsigned long sub = 0; sub < (1UL <<
m); ++sub)
230 std::vector<bool> used(
size_t(s.
n),
false);
233 for (
size_t i = 0; i <
m and ok; ++i)
234 if (sub & (1UL << i))
236 const int u = s.
arcs[i][0], v = s.
arcs[i][1];
237 if (u == v or used[
size_t(u)] or used[
size_t(v)])
241 used[size_t(u)] = used[size_t(v)] =
true;
246 best = std::max(best,
size);
254 std::vector<bool> used(
size_t(s.
n),
false);
257 const int u = s.
arcs[size_t(i)][0], v = s.
arcs[size_t(i)][1];
258 if (u == v or used[
size_t(u)] or used[
size_t(v)])
260 used[size_t(u)] = used[size_t(v)] =
true;
268 std::vector<size_t> order(s.
arcs.size());
269 std::iota(order.begin(), order.end(),
size_t(0));
270 std::stable_sort(order.begin(), order.end(),
271 [&] (
size_t a,
size_t b) { return s.w[a] < s.w[b]; });
274 for (
size_t i : order)
282 inline std::vector<std::vector<bool>>
closure(
const Spec & s,
bool directed)
284 std::vector<std::vector<bool>>
r(
size_t(s.
n), std::vector<bool>(
size_t(s.
n),
false));
285 for (
const auto & a : s.
arcs)
287 r[size_t(a[0])][size_t(a[1])] =
true;
289 r[size_t(a[1])][size_t(a[0])] =
true;
291 for (
int k = 0;
k < s.
n; ++
k)
292 for (
int i = 0; i < s.
n; ++i)
293 if (
r[
size_t(i)][
size_t(
k)])
294 for (
int j = 0; j < s.
n; ++j)
295 if (
r[
size_t(
k)][
size_t(j)])
296 r[size_t(i)][size_t(j)] =
true;
301 inline bool reaches(
const std::vector<std::vector<bool>> &
r,
int u,
int v)
303 return u == v or
r[size_t(u)][size_t(v)];
311 std::vector<int> part(
size_t(s.
n));
312 for (
int u = 0; u < s.
n; ++u)
315 for (
int v = 0; v < u; ++v)
316 if (
r[
size_t(u)][
size_t(v)]
and r[
size_t(v)][
size_t(u)])
318 part[size_t(u)] = part[size_t(v)];
329 for (
int u = 0; u < s.
n; ++u)
330 if (
r[
size_t(u)][
size_t(u)])
338 if (
int(order.size()) != s.
n)
340 std::vector<int> pos(
size_t(s.
n), -1);
341 for (
size_t i = 0; i < order.size(); ++i)
343 if (order[i] < 0 or order[i] >= s.
n or pos[
size_t(order[i])] != -1)
345 pos[size_t(order[i])] = int(i);
347 for (
const auto & a : s.
arcs)
348 if (pos[
size_t(a[0])] >= pos[size_t(a[1])])
355 inline std::vector<std::vector<int>>
hops(
const Spec & s,
bool directed)
357 std::vector<std::vector<int>>
h(
size_t(s.
n), std::vector<int>(
size_t(s.
n), -1));
358 for (
int u = 0; u < s.
n; ++u)
360 auto & d =
h[size_t(u)];
362 std::vector<int> queue = {u};
363 for (
size_t k = 0;
k < queue.size(); ++
k)
364 for (
const auto & a : s.
arcs)
365 for (
size_t e = 0; e < (directed ? 1U : 2U); ++e)
366 if (a[e] == queue[
k]
and d[
size_t(a[1 - e])] == -1)
368 d[size_t(a[1 - e])] = d[size_t(queue[
k])] + 1;
369 queue.push_back(a[1 - e]);
379 for (
auto & a :
r.arcs)
380 std::swap(a[0], a[1]);
391 for (
size_t i = 0; i < s.
arcs.size(); ++i)
392 if (((hidden >> i) & 1UL) == 0)
394 r.arcs.push_back(s.
arcs[i]);
396 r.w.push_back(s.
w[i]);
397 orig.push_back(
int(i));
408 std::vector<int> rank(
size_t(s.
n), 0);
409 for (
int u = 0; u < s.
n; ++u)
410 if (
r[
size_t(u)][
size_t(u)])
411 for (
int v = 0; v < s.
n; ++v)
413 rank[size_t(v)] = -1;
416 for (
int round = 0; round < s.
n; ++round)
417 for (
const auto & a : s.
arcs)
418 if (rank[
size_t(a[0])] != -1
and rank[size_t(a[1])] != -1)
419 rank[
size_t(a[1])] = std::max(rank[
size_t(a[1])], rank[
size_t(a[0])] + 1);
429 auto reached = [&] (
int skip)
431 std::vector<bool> seen(
size_t(s.
n),
false);
434 seen[size_t(
root)] =
true;
435 for (
bool grew =
true; grew; )
438 for (
const auto & a : s.
arcs)
439 if (seen[
size_t(a[0])]
and a[1] != skip
and not seen[size_t(a[1])])
441 seen[size_t(a[1])] =
true;
448 const auto all = reached(-1);
449 std::vector<std::vector<bool>> dom(
size_t(s.
n), std::vector<bool>(
size_t(s.
n),
false));
450 for (
int d = 0; d < s.
n; ++d)
452 if (not
all[
size_t(d)])
454 const auto rest = reached(d);
455 for (
int v = 0; v < s.
n; ++v)
456 dom[
size_t(d)][size_t(v)] =
all[size_t(v)]
and (d == v or not rest[
size_t(v)]);
466 std::vector<int> idom(
size_t(s.
n), -1);
467 for (
int v = 0; v < s.
n; ++v)
468 for (
int d = 0; d < s.
n; ++d)
470 if (d == v or not dom[
size_t(d)][
size_t(v)])
473 for (
int e = 0; e < s.
n and deepest; ++e)
474 if (e != v
and dom[
size_t(e)][
size_t(v)])
475 deepest = dom[size_t(e)][size_t(d)];
487 std::vector<std::set<int>>
df(
size_t(s.
n));
488 for (
int x = 0; x < s.
n; ++x)
489 for (
const auto & a : s.
arcs)
490 if (dom[
size_t(x)][size_t(a[0])]
491 and (x == a[1] or not dom[
size_t(x)][
size_t(a[1])]))
492 df[
size_t(x)].insert(a[1]);
500 bool directed,
bool closed)
504 std::vector<bool> used(s.
arcs.size(),
false);
507 if (i < 0 or
size_t(i) >= s.
arcs.size() or used[size_t(i)])
509 used[size_t(i)] =
true;
519 const int u = s.
arcs[size_t(i)][0], v = s.
arcs[size_t(i)][1];
522 else if (not directed
and v == at)
530 if (ok
and (not closed or at == start))
549 for (
const auto & a : s.
arcs)
552 for (
const auto & a : s.
arcs)
553 roots.insert(uf.
find(a[0]));
554 if (roots.size() > 1)
560 for (
int x = 0; x < s.
n; ++x)
566 for (
int x = 0; x < s.
n; ++x)
569 for (
const auto & a : s.
arcs)
578 else if (in ==
out + 1)
589 constexpr long long Inf = std::numeric_limits<long long>::max();
594 if (b > 0
and a > std::numeric_limits<long long>::max() - b)
595 return std::numeric_limits<long long>::max();
596 if (b < 0
and a < std::numeric_limits<long long>::lowest() - b)
597 return std::numeric_limits<long long>::lowest();
606 inline std::vector<std::vector<long long>>
609 std::vector<std::vector<long long>> d(
size_t(s.
n), std::vector<long long>(
size_t(s.
n),
Inf));
610 for (
int i = 0; i < s.
n; ++i)
611 d[
size_t(i)][size_t(i)] = 0;
612 for (
size_t i = 0; i < s.
arcs.size(); ++i)
614 const size_t u = size_t(s.
arcs[i][0]), v = size_t(s.
arcs[i][1]);
615 d[u][v] = std::min(d[u][v], s.
w[i]);
617 d[v][u] = std::min(d[v][u], s.
w[i]);
619 for (
size_t k = 0;
k < size_t(s.
n); ++
k)
620 for (
size_t i = 0; i < size_t(s.
n); ++i)
621 for (
size_t j = 0; j < size_t(s.
n); ++j)
625 for (
size_t i = 0; i < size_t(s.
n); ++i)
626 neg = neg or d[i][i] < 0;
638 const int first = s.
arcs[size_t(
arcs[0])][0];
641 for (
int start : {first, s.
arcs[size_t(
arcs[0])][1]})
648 const int u = s.
arcs[size_t(i)][0], v = s.
arcs[size_t(i)][1];
651 else if (not directed
and v == at)
658 total += s.
w[size_t(i)];
660 if (ok
and at == start
and total < 0)
672 std::vector<std::vector<long long>> c(
size_t(s.
n), std::vector<long long>(
size_t(s.
n), 0));
673 for (
size_t i = 0; i < s.
arcs.size(); ++i)
675 c[size_t(s.
arcs[i][0])][size_t(s.
arcs[i][1])] += s.
w[i];
679 std::vector<int> parent(
size_t(s.
n), -1);
680 parent[size_t(src)] = src;
681 std::vector<int> queue = {src};
682 for (
size_t h = 0;
h < queue.size(); ++
h)
683 for (
int v = 0; v < s.
n; ++v)
684 if (parent[
size_t(v)] == -1
and c[
size_t(queue[
h])][
size_t(v)] > 0)
686 parent[size_t(v)] = queue[
h];
689 if (parent[
size_t(snk)] == -1)
692 for (
int v = snk; v != src; v = parent[size_t(v)])
693 b = std::min(b, c[
size_t(parent[
size_t(v)])][size_t(v)]);
694 for (
int v = snk; v != src; v = parent[size_t(v)])
696 c[size_t(parent[
size_t(v)])][size_t(v)] -= b;
697 c[size_t(v)][size_t(parent[
size_t(v)])] += b;
size_t size_t int32_t * out
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Every small multigraph, on every kind of graph, for the tests.
DynArray< Graph::Arc * > arcs
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
and
Check uniqueness with explicit hash + equality functors.
static long & df(typename GT::Node *p)
Internal helper: DFS discovery time stored in NODE_COUNTER(p).
static long & color(typename GT::Node *p)
bool is_dag(const Spec &s)
Whether the digraph has no directed cycle (a loop is one)
constexpr long long Inf
No distance, in the matrices of the oracles.
bool is_matching(const Spec &s, const std::vector< int > &arcs)
Whether the arcs, by number, form a matching of s.
bool has_cycle(const Spec &s)
Whether the multigraph, not directed, has a cycle.
std::vector< std::vector< int > > hops(const Spec &s, bool directed)
hops[u][v]: the least number of arcs of a path from u to v, 0 from u to itself and -1 if there is no ...
int max_matching(const Spec &s)
The size of a maximum matching, by brute force over the subsets of the arcs; loops never match.
std::vector< std::vector< long long > > shortest_distances(const Spec &s, bool directed, bool &neg)
Floyd on a matrix of long long: the shortest distances, Inf where there is no path,...
Euler euler_class(const Spec &s, bool directed)
The Eulerian class of s: the arcs must be in one component (weakly, in a digraph),...
std::vector< std::vector< bool > > dominators(const Spec &s, int root)
dom[d][v]: d dominates v from root in a digraph, that is, v is reached from root and every path from ...
std::vector< std::vector< bool > > closure(const Spec &s, bool directed)
reach[u][v]: v is reached from u by a directed path of one arc or more (in a graph that is not direct...
std::vector< std::set< int > > dominance_frontiers(const Spec &s, int root)
The dominance frontier of each node from root: y is in that of x if x dominates a predecessor of y an...
long long min_spanning_forest_weight(const Spec &s)
The weight of a minimum spanning forest: Kruskal on the list of arcs.
std::vector< int > component_of(const Spec &s, int skip_node=-1, int skip_arc=-1)
The number of the component of each node, as that of its root, of the graph without the node skip_nod...
long long max_flow(const Spec &s, int src, int snk)
The maximum flow from src to snk, by Edmonds-Karp on the matrix of the capacities w (loops carry noth...
bool reaches(const std::vector< std::vector< bool > > &r, int u, int v)
Whether there is a path, of zero arcs or more, from u to v.
std::vector< int > kahn_ranks(const Spec &s)
The rank that Kahn's algorithm gives each node of a digraph: 0 for a node without arcs in,...
std::set< int > cut_nodes(const Spec &s)
The nodes whose removal splits their component.
std::set< int > bridges(const Spec &s)
The arcs, by number, whose removal splits their component.
int components(const Spec &s, int skip_node=-1, int skip_arc=-1)
The number of components, without the node skip_node and the arc skip_arc.
bool on_cycle(const Spec &s, int x)
Whether the node x lies on a cycle of the multigraph, not directed.
bool is_topological_order(const Spec &s, const std::vector< int > &order)
Whether order, a sequence of all the nodes, is a topological order.
bool is_bipartite(const Spec &s)
Whether the multigraph, not directed, has two colors with no arc inside one (a loop is an odd cycle)
long long saturated_add(long long a, long long b)
a + b, kept within the range of long long
Spec without(const Spec &s, unsigned long hidden, std::vector< int > &orig)
The spec without the arcs whose numbers are set in hidden, with orig[i] the number in s of its i-th a...
int degree(const Spec &s, int x)
The degree of x: a loop counts 2.
Euler
What kind of Eulerian walk the multigraph has.
bool is_euler_walk(const Spec &s, const std::vector< int > &arcs, bool directed, bool closed)
Whether arcs, by number, walk every arc of s once, each from the node where the one before ended (fro...
Spec reversed(const Spec &s)
The spec with each arc reversed; the arcs keep their numbers.
std::vector< int > immediate_dominators(const Spec &s, int root)
The immediate dominator of each node from root: the strict dominator that all the others dominate; -1...
bool is_negative_cycle(const Spec &s, const std::vector< int > &arcs, bool directed)
Whether the arcs, by number, form a closed walk of s whose weights add up to less than zero; in a gra...
std::vector< int > scc_partition(const Spec &s)
The strongly connected components, as the smallest node of the component of each node.
A small multigraph: its nodes are 0 .
std::vector< long long > w
std::vector< std::array< int, 2 > > arcs
Union-find over the nodes 0 .. n - 1.
bool join(int a, int b)
Joins the sets of a and b; false if they were the same set.
std::vector< int > parent
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)