競技プログラミング用 C++ ライブラリ
graph/bipartite_graph.hpp · GitHub で見る
共通ページ
無向グラフの隣接リスト vector<vector<int>> を渡し、二部判定・色分け・最大マッチング・最大独立集合・最小頂点被覆を求めます。左右の頂点番号を付け直す必要はなく、結果はすべて元の頂点番号です。
#include "lib/graph/bipartite_graph.hpp"
int main() {
std::vector<std::vector<int>> g(5);
for (auto [u, v] : std::vector<std::pair<int, int>>{ {0, 1}, {1, 2}, {2, 3}, {3, 0} }) {
g[u].push_back(v);
g[v].push_back(u);
}
BipartiteGraph graph(g);
if (!graph.is_bipartite()) return 0;
auto independent = graph.maximum_independent_set();
auto cover = graph.minimum_vertex_cover();
auto matching = graph.maximum_matching();
assert(independent.size() == 3);
assert(cover.size() == 2 && matching.size() == 2);
}
各無向辺は両端の隣接リストへ追加します。頂点番号は \([0,N)\)、頂点数は INT_MAX 未満、辺数は INT_MAX 以下です。非連結グラフ・孤立頂点・多重辺・空グラフに対応します。自己ループや奇数長の閉路があれば is_bipartite() は偽になります。
二部グラフでない場合、size()・empty()・is_bipartite()・再構築の build(g) を使えます。色分けや集合を取得する操作は、二部グラフであることを確認してから呼びます。入力を変更せず、構築後は入力を参照しません。
\(N\) を頂点数、\(M\) を無向辺数、\(K\) を色分け後の小さい側の頂点数とします。集合の取得時に必要なら最大マッチングを計算し、その結果を再利用します。下表の取得操作の計算量は、最大マッチングを計算済みの場合です。
| 操作 | 内容・返り値 | 計算量 |
|---|---|---|
BipartiteGraph(g) |
無向隣接リストから構築し、二部判定・色分けを行う | \(O(N+M)\) |
build(g) |
指定したグラフで構築し直し、二部グラフなら真を返す | \(O(N+M)\) |
size() / empty()
|
頂点数/頂点数が 0 か | \(O(1)\) |
is_bipartite() |
二部グラフか | \(O(1)\) |
coloring() |
各頂点の色 0 または 1 の const vector<int>&
|
\(O(1)\) |
matching_size() |
最大マッチングの辺数 | \(O(1)\) |
maximum_matching() |
最大マッチングの頂点対 (色0,色1) の列 |
\(O(N)\) |
mates() |
各頂点の対応相手。未対応は -1 | \(O(N)\) |
minimum_vertex_cover() |
全辺を少なくとも片端で覆う最小の頂点集合 | \(O(N+M)\) |
maximum_independent_set() |
選んだ頂点間に辺がない最大の頂点集合 | \(O(N+M)\) |
minimum_edge_cover() |
全頂点を覆う最小の頂点対の列。孤立頂点があれば nullopt
|
\(O(N+M)\) |
最大独立集合の頂点数は \(N-\lvert\mathrm{matching}\rvert\)、最小頂点被覆の頂点数は最大マッチングの辺数です。両者は互いの補集合になります。最小辺被覆が存在する場合、その辺数も \(N-\lvert\mathrm{matching}\rvert\) です。空グラフの最小辺被覆は空の列です。
左右を指定して辺を追加したり、Hall 条件の反例・辺の分類を求めたりする場合は 二部マッチング を使います。このライブラリも同じマッチング処理を利用します。
構築は \(O(N+M)\)、最大マッチングの計算は \(K>0\) なら \(O((N+M)\sqrt K)\) 時間、\(K=0\) なら \(O(1)\) 時間です。保存領域は \(O(N+M)\)、集合の復元は \(O(N+M)\) 時間・追加空間です。
#ifndef BIPARTITE_GRAPH_HPP
#define BIPARTITE_GRAPH_HPP
#include "bipartite_matching.hpp"
#include <cassert>
#include <cstddef>
#include <limits>
#include <optional>
#include <utility>
#include <vector>
class BipartiteGraph {
bool bipartite_ = true;
std::vector<int> color_, left_, right_;
BipartiteMatching matching_{0, 0};
std::vector<int> restore(BipartiteMatching::VertexSet vertices) const {
std::vector<int> result;
result.reserve(vertices.left.size() + vertices.right.size());
for (int v : vertices.left) result.push_back(left_[v]);
for (int v : vertices.right) result.push_back(right_[v]);
return result;
}
public:
BipartiteGraph() = default;
explicit BipartiteGraph(const std::vector<std::vector<int>>& graph) { build(graph); }
bool build(const std::vector<std::vector<int>>& graph) {
assert(graph.size() < std::size_t(std::numeric_limits<int>::max()));
int n = static_cast<int>(graph.size());
bipartite_ = true;
color_.assign(n, -1);
left_.clear(), right_.clear();
matching_ = BipartiteMatching(0, 0);
std::vector<int> queue;
queue.reserve(n);
for (int root = 0; root < n; ++root) if (color_[root] < 0) {
color_[root] = 0;
queue.clear();
queue.push_back(root);
for (std::size_t head = 0; head < queue.size(); ++head) {
int v = queue[head];
for (int to : graph[v]) {
assert(0 <= to && to < n);
if (color_[to] < 0) color_[to] = color_[v] ^ 1, queue.push_back(to);
else if (color_[to] == color_[v]) return bipartite_ = false;
}
}
}
std::vector<int> index(n);
std::size_t m = 0;
for (int v = 0; v < n; ++v) {
auto& side = color_[v] ? right_ : left_;
index[v] = static_cast<int>(side.size());
side.push_back(v);
if (!color_[v]) {
assert(graph[v].size() <= std::size_t(std::numeric_limits<int>::max()) - m);
m += graph[v].size();
}
}
matching_ = BipartiteMatching(static_cast<int>(left_.size()), static_cast<int>(right_.size()));
matching_.reserve(m);
for (int v : left_) for (int to : graph[v]) matching_.add_edge(index[v], index[to]);
return true;
}
int size() const { return static_cast<int>(color_.size()); }
bool empty() const { return color_.empty(); }
bool is_bipartite() const { return bipartite_; }
const std::vector<int>& coloring() const { assert(bipartite_); return color_; }
int matching_size() { assert(bipartite_); return matching_.build(); }
std::vector<std::pair<int, int>> maximum_matching() {
assert(bipartite_);
auto result = matching_.edges();
for (auto& [u, v] : result) u = left_[u], v = right_[v];
return result;
}
std::vector<int> mates() {
assert(bipartite_);
std::vector<int> result(size(), -1);
for (auto [u, v] : maximum_matching()) result[u] = v, result[v] = u;
return result;
}
std::vector<int> minimum_vertex_cover() {
assert(bipartite_);
return restore(matching_.minimum_vertex_cover());
}
std::vector<int> maximum_independent_set() {
assert(bipartite_);
return restore(matching_.maximum_independent_set());
}
std::optional<std::vector<std::pair<int, int>>> minimum_edge_cover() {
assert(bipartite_);
auto ids = matching_.minimum_edge_cover();
if (!ids) return std::nullopt;
std::vector<std::pair<int, int>> result;
result.reserve(ids->size());
for (int id : *ids) {
auto [u, v] = matching_.get_edge(id);
result.emplace_back(left_[u], right_[v]);
}
return result;
}
};
#endif
#line 1 "graph/bipartite_graph.hpp"
#line 1 "graph/bipartite_matching.hpp"
#line 1 "components/bipartite_matching_core.hpp"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <limits>
#include <numeric>
#include <span>
#include <utility>
#include <vector>
namespace bipartite_matching_detail {
struct CSR {
std::vector<int> start, adj;
CSR(int n, std::span<const std::pair<int, int>> edges, bool transpose) : start(n + 1), adj(edges.size()) {
for (auto [u, v] : edges) ++start[(transpose ? v : u) + 1];
std::partial_sum(start.begin(), start.end(), start.begin());
auto next = start;
for (auto [u, v] : edges) adj[next[transpose ? v : u]++] = transpose ? u : v;
}
CSR(int n, const CSR& graph) : start(n + 1), adj(graph.adj.size()) {
for (int v : graph.adj) ++start[v + 1];
std::partial_sum(start.begin(), start.end(), start.begin());
auto next = start;
for (int u = 0; u + 1 < int(graph.start.size()); ++u)
for (int e = graph.start[u]; e < graph.start[u + 1]; ++e) adj[next[graph.adj[e]]++] = u;
}
};
struct Result { int added; bool maximum; };
inline Result hopcroft_karp(const CSR& graph, std::vector<int>& left, std::vector<int>& right,
int phases = std::numeric_limits<int>::max(), int max_distance = std::numeric_limits<int>::max()) {
int n = int(left.size()), added = 0;
std::vector<int> distance(n), queue(n), next(n), path(n);
for (int phase = 0; phase < phases; ++phase) {
int tail = 0, limit = n + 1;
for (int u = 0; u < n; ++u) {
distance[u] = left[u] < 0 ? 0 : -1;
if (left[u] < 0) queue[tail++] = u;
}
for (int head = 0; head < tail; ++head) {
int u = queue[head];
if (distance[u] >= limit) break;
for (int e = graph.start[u]; e < graph.start[u + 1]; ++e) {
int w = right[graph.adj[e]];
if (w < 0) limit = distance[u] + 1;
else if (distance[w] < 0) distance[w] = distance[u] + 1, queue[tail++] = w;
}
}
if (limit == n + 1) return {added, true};
if (limit > max_distance) return {added, false};
std::copy_n(graph.start.begin(), n, next.begin());
for (int root = 0; root < n; ++root) if (left[root] < 0 && distance[root] == 0) {
int depth = 1;
path[0] = root;
while (depth) {
int u = path[depth - 1];
bool descend = false, found = false;
while (next[u] < graph.start[u + 1]) {
int v = graph.adj[next[u]++], w = right[v];
if (w < 0) {
if (distance[u] + 1 != limit) continue;
while (depth) {
int x = path[--depth], previous = left[x];
left[x] = v, right[v] = x;
distance[x] = -1;
v = previous;
}
++added;
found = true;
break;
}
if (distance[w] == distance[u] + 1 && distance[w] < limit) {
path[depth++] = w;
descend = true;
break;
}
}
if (found) break;
if (!descend) distance[u] = -1, --depth;
}
}
}
return {added, false};
}
inline int warm_start(const CSR& graph, std::vector<int>& left, std::vector<int>& right, int multiplier) {
assert(multiplier >= 0);
int l = int(left.size()), r = int(right.size()), n = l + r, added = 0;
int deficit = int(std::count(left.begin(), left.end(), -1));
if (!deficit) return 0;
CSR reverse(r, graph);
std::vector<int> dl(l), dr(r), bfs(l), active(r);
std::uint64_t work = 0, budget = std::uint64_t(graph.adj.size() + n) * multiplier;
int head = 0, tail = 0, count = 0, steps = 0;
bool first = true;
for (;;) {
if (!steps) {
work += n;
if (work > budget) return added;
int end = 0;
std::fill(dr.begin(), dr.end(), n);
for (int u = 0; u < l; ++u) {
dl[u] = left[u] < 0 ? 0 : n;
if (left[u] < 0) bfs[end++] = u;
}
for (int begin = 0; begin < end; ++begin) {
int u = bfs[begin];
work += graph.start[u + 1] - graph.start[u];
if (work > budget) return added;
for (int e = graph.start[u]; e < graph.start[u + 1]; ++e) {
int v = graph.adj[e];
if (dr[v] <= dl[u] + 1) continue;
dr[v] = dl[u] + 1;
int w = right[v];
if (w >= 0 && dl[w] == n) dl[w] = dr[v] + 1, bfs[end++] = w;
}
}
if (first) {
for (int v = 0; v < r; ++v) if (right[v] < 0 && dr[v] < n) active[count++] = v;
tail = count == r ? 0 : count;
first = false;
}
}
if (!count) return added;
int v = active[head++], best = -1, minimum = n;
if (head == r) head = 0;
--count;
work += reverse.start[v + 1] - reverse.start[v] + 1;
if (work > budget) return added;
for (int e = reverse.start[v]; e < reverse.start[v + 1]; ++e) {
int u = reverse.adj[e];
if (dl[u] < minimum) minimum = dl[u], best = u;
}
if (best >= 0) {
int previous = left[best];
if (previous < 0) ++added;
else {
right[previous] = -1;
active[tail++] = previous;
if (tail == r) tail = 0;
++count;
}
left[best] = v, right[v] = best;
dl[best] += 2;
if (added == deficit) return added;
}
if (++steps == n) steps = 0;
}
}
inline int augment(const CSR& graph, std::vector<int>& left, std::vector<int>& right,
int multiplier = 16, int root_divisor = 32, int easy_phases = 8) {
int roots = 0, n = int(left.size());
for (int u = 0; u < n; ++u) roots += left[u] < 0 && graph.start[u] != graph.start[u + 1];
Result initial{0, false};
if (root_divisor > 0 && roots <= std::max(64, n / root_divisor)) initial = hopcroft_karp(graph, left, right, easy_phases);
if (initial.maximum || std::find(left.begin(), left.end(), -1) == left.end()) return initial.added;
int added = initial.added + warm_start(graph, left, right, multiplier);
if (std::find(left.begin(), left.end(), -1) == left.end()) return added;
return added + hopcroft_karp(graph, left, right).added;
}
struct Analysis {
int left_size;
std::vector<int> start, adj, reverse_start, reverse_adj, component, matched_edge;
std::vector<unsigned char> from_left, to_right;
Analysis(int l, int r, std::span<const std::pair<int, int>> edges,
const std::vector<int>& left, const std::vector<int>& right)
: left_size(l), start(l + r + 1), adj(edges.size()), reverse_start(l + r + 1),
reverse_adj(edges.size()), matched_edge(l, -1), from_left(l + r), to_right(l + r) {
for (int i = 0; i < int(edges.size()); ++i) {
auto [u, v] = edges[i];
if (left[u] == v && matched_edge[u] < 0) matched_edge[u] = i;
v += l;
if (matched_edge[u] == i) std::swap(u, v);
++start[u + 1], ++reverse_start[v + 1];
}
std::partial_sum(start.begin(), start.end(), start.begin());
std::partial_sum(reverse_start.begin(), reverse_start.end(), reverse_start.begin());
auto next = start, reverse_next = reverse_start;
for (int i = 0; i < int(edges.size()); ++i) {
auto [u, v] = edges[i];
v += l;
if (matched_edge[u] == i) std::swap(u, v);
adj[next[u]++] = v, reverse_adj[reverse_next[v]++] = u;
}
std::vector<int> queue(l + r);
int tail = 0;
for (int u = 0; u < l; ++u) if (left[u] < 0) queue[tail++] = u, from_left[u] = 1;
reach(queue, tail, start, adj, from_left);
tail = 0;
for (int v = 0; v < r; ++v) if (right[v] < 0) queue[tail++] = l + v, to_right[l + v] = 1;
reach(queue, tail, reverse_start, reverse_adj, to_right);
}
static void reach(std::vector<int>& queue, int tail, const std::vector<int>& start,
const std::vector<int>& adj, std::vector<unsigned char>& seen) {
for (int head = 0; head < tail; ++head) {
int u = queue[head];
for (int e = start[u]; e < start[u + 1]; ++e) if (!seen[adj[e]]) {
int v = adj[e];
seen[v] = 1, queue[tail++] = v;
}
}
}
void components() {
int n = int(from_left.size());
if (!component.empty() || !n) return;
std::vector<unsigned char> seen(n);
std::vector<int> order, stack, next = start;
order.reserve(n), stack.reserve(n);
for (int root = 0; root < n; ++root) if (!seen[root]) {
seen[root] = 1;
stack.push_back(root);
while (!stack.empty()) {
int u = stack.back();
if (next[u] == start[u + 1]) order.push_back(u), stack.pop_back();
else {
int v = adj[next[u]++];
if (!seen[v]) seen[v] = 1, stack.push_back(v);
}
}
}
component.assign(n, -1);
int count = 0;
for (auto it = order.rbegin(); it != order.rend(); ++it) if (component[*it] < 0) {
component[*it] = count;
stack.push_back(*it);
while (!stack.empty()) {
int u = stack.back();
stack.pop_back();
for (int e = reverse_start[u]; e < reverse_start[u + 1]; ++e) {
int v = reverse_adj[e];
if (component[v] < 0) component[v] = count, stack.push_back(v);
}
}
++count;
}
}
};
}
#line 5 "graph/bipartite_matching.hpp"
#include <cstddef>
#line 8 "graph/bipartite_matching.hpp"
#include <optional>
class BipartiteMatching {
public:
struct VertexSet { std::vector<int> left, right; };
enum class EdgeStatus : unsigned char { never, optional, forced };
private:
int l_, r_, size_ = 0;
bool dirty_ = false;
std::vector<std::pair<int, int>> input_;
std::vector<int> left_, right_;
std::optional<bipartite_matching_detail::Analysis> analysis_;
auto& analysis() {
build();
if (!analysis_) analysis_.emplace(l_, r_, input_, left_, right_);
return *analysis_;
}
template<bool left_selected, bool right_selected>
VertexSet select_vertices(const std::vector<unsigned char>& mask) const {
VertexSet result;
for (int u = 0; u < l_; ++u) if (bool(mask[u]) == left_selected) result.left.push_back(u);
for (int v = 0; v < r_; ++v) if (bool(mask[l_ + v]) == right_selected) result.right.push_back(v);
return result;
}
public:
explicit BipartiteMatching(int l, int r) : l_(l), r_(r), left_(l, -1), right_(r, -1) {
assert(l >= 0 && r >= 0 && std::int64_t(l) + r < std::numeric_limits<int>::max());
}
int left_size() const { return l_; }
int right_size() const { return r_; }
int edge_count() const { return int(input_.size()); }
void reserve(std::size_t m) { assert(m <= std::size_t(std::numeric_limits<int>::max())); input_.reserve(m); }
int add_edge(int u, int v) {
assert(0 <= u && u < l_ && 0 <= v && v < r_ && input_.size() < std::size_t(std::numeric_limits<int>::max()));
int id = edge_count();
input_.emplace_back(u, v);
if (left_[u] < 0 && right_[v] < 0) left_[u] = v, right_[v] = u, ++size_;
dirty_ = true;
analysis_.reset();
return id;
}
std::pair<int, int> get_edge(int id) const { assert(0 <= id && id < edge_count()); return input_[id]; }
int build() {
if (!dirty_) return size_;
if (size_ < std::min(l_, r_)) {
bool transpose = l_ > r_;
bipartite_matching_detail::CSR graph(transpose ? r_ : l_, input_, transpose);
size_ += bipartite_matching_detail::augment(graph, transpose ? right_ : left_, transpose ? left_ : right_);
}
dirty_ = false;
return size_;
}
const std::vector<int>& left_mates() { build(); return left_; }
const std::vector<int>& right_mates() { build(); return right_; }
bool covers_left() { return build() == l_; }
bool covers_right() { return build() == r_; }
std::vector<std::pair<int, int>> edges() {
build();
std::vector<std::pair<int, int>> result;
result.reserve(size_);
for (int u = 0; u < l_; ++u) if (left_[u] >= 0) result.emplace_back(u, left_[u]);
return result;
}
std::vector<int> matching_edge_ids() {
build();
std::vector<int> ids(l_, -1), result;
for (int i = 0; i < edge_count(); ++i) {
auto [u, v] = input_[i];
if (left_[u] == v && ids[u] < 0) ids[u] = i;
}
result.reserve(size_);
for (int id : ids) if (id >= 0) result.push_back(id);
return result;
}
VertexSet minimum_vertex_cover() { return select_vertices<false, true>(analysis().from_left); }
VertexSet maximum_independent_set() { return select_vertices<true, false>(analysis().from_left); }
std::optional<std::vector<int>> minimum_edge_cover() {
build();
std::vector<int> first_left(l_, -1), first_right(r_, -1);
for (int i = 0; i < edge_count(); ++i) {
auto [u, v] = input_[i];
if (first_left[u] < 0) first_left[u] = i;
if (first_right[v] < 0) first_right[v] = i;
}
if (std::find(first_left.begin(), first_left.end(), -1) != first_left.end() ||
std::find(first_right.begin(), first_right.end(), -1) != first_right.end()) return std::nullopt;
auto result = matching_edge_ids();
for (int u = 0; u < l_; ++u) if (left_[u] < 0) result.push_back(first_left[u]);
for (int v = 0; v < r_; ++v) if (right_[v] < 0) result.push_back(first_right[v]);
return result;
}
std::optional<VertexSet> hall_witness_left() {
if (covers_left()) return std::nullopt;
return select_vertices<true, true>(analysis().from_left);
}
std::optional<VertexSet> hall_witness_right() {
if (covers_right()) return std::nullopt;
return select_vertices<true, true>(analysis().to_right);
}
std::vector<EdgeStatus> edge_classification() {
auto& a = analysis();
a.components();
std::vector<EdgeStatus> result(edge_count());
for (int i = 0; i < edge_count(); ++i) {
auto [u, v] = input_[i];
bool flexible = a.from_left[u] || a.to_right[l_ + v] || a.component[u] == a.component[l_ + v];
result[i] = flexible ? EdgeStatus::optional : a.matched_edge[u] == i ? EdgeStatus::forced : EdgeStatus::never;
}
return result;
}
void clear() {
input_.clear();
std::fill(left_.begin(), left_.end(), -1);
std::fill(right_.begin(), right_.end(), -1);
size_ = 0;
dirty_ = false;
analysis_.reset();
}
};
#line 11 "graph/bipartite_graph.hpp"
class BipartiteGraph {
bool bipartite_ = true;
std::vector<int> color_, left_, right_;
BipartiteMatching matching_{0, 0};
std::vector<int> restore(BipartiteMatching::VertexSet vertices) const {
std::vector<int> result;
result.reserve(vertices.left.size() + vertices.right.size());
for (int v : vertices.left) result.push_back(left_[v]);
for (int v : vertices.right) result.push_back(right_[v]);
return result;
}
public:
BipartiteGraph() = default;
explicit BipartiteGraph(const std::vector<std::vector<int>>& graph) { build(graph); }
bool build(const std::vector<std::vector<int>>& graph) {
assert(graph.size() < std::size_t(std::numeric_limits<int>::max()));
int n = static_cast<int>(graph.size());
bipartite_ = true;
color_.assign(n, -1);
left_.clear(), right_.clear();
matching_ = BipartiteMatching(0, 0);
std::vector<int> queue;
queue.reserve(n);
for (int root = 0; root < n; ++root) if (color_[root] < 0) {
color_[root] = 0;
queue.clear();
queue.push_back(root);
for (std::size_t head = 0; head < queue.size(); ++head) {
int v = queue[head];
for (int to : graph[v]) {
assert(0 <= to && to < n);
if (color_[to] < 0) color_[to] = color_[v] ^ 1, queue.push_back(to);
else if (color_[to] == color_[v]) return bipartite_ = false;
}
}
}
std::vector<int> index(n);
std::size_t m = 0;
for (int v = 0; v < n; ++v) {
auto& side = color_[v] ? right_ : left_;
index[v] = static_cast<int>(side.size());
side.push_back(v);
if (!color_[v]) {
assert(graph[v].size() <= std::size_t(std::numeric_limits<int>::max()) - m);
m += graph[v].size();
}
}
matching_ = BipartiteMatching(static_cast<int>(left_.size()), static_cast<int>(right_.size()));
matching_.reserve(m);
for (int v : left_) for (int to : graph[v]) matching_.add_edge(index[v], index[to]);
return true;
}
int size() const { return static_cast<int>(color_.size()); }
bool empty() const { return color_.empty(); }
bool is_bipartite() const { return bipartite_; }
const std::vector<int>& coloring() const { assert(bipartite_); return color_; }
int matching_size() { assert(bipartite_); return matching_.build(); }
std::vector<std::pair<int, int>> maximum_matching() {
assert(bipartite_);
auto result = matching_.edges();
for (auto& [u, v] : result) u = left_[u], v = right_[v];
return result;
}
std::vector<int> mates() {
assert(bipartite_);
std::vector<int> result(size(), -1);
for (auto [u, v] : maximum_matching()) result[u] = v, result[v] = u;
return result;
}
std::vector<int> minimum_vertex_cover() {
assert(bipartite_);
return restore(matching_.minimum_vertex_cover());
}
std::vector<int> maximum_independent_set() {
assert(bipartite_);
return restore(matching_.maximum_independent_set());
}
std::optional<std::vector<std::pair<int, int>>> minimum_edge_cover() {
assert(bipartite_);
auto ids = matching_.minimum_edge_cover();
if (!ids) return std::nullopt;
std::vector<std::pair<int, int>> result;
result.reserve(ids->size());
for (int id : *ids) {
auto [u, v] = matching_.get_edge(id);
result.emplace_back(left_[u], right_[v]);
}
return result;
}
};