24 const std::vector<std::vector<int>>& left_to_right_arcs,
int num_right) {
32 const int num_left = left_to_right_arcs.size();
33 std::vector<SimpleMaxFlow::ArcIndex> arcs;
34 for (
int i = 0; i < num_left; ++i) {
35 for (
const int right_node : left_to_right_arcs[i]) {
36 DCHECK_GE(right_node, num_left);
37 DCHECK_LT(right_node, num_right + num_left);
41 std::vector<std::vector<int>> adj_list = left_to_right_arcs;
42 adj_list.resize(num_left + num_right);
43 for (
int i = 0; i < num_left; ++i) {
44 for (
const int right_node : left_to_right_arcs[i]) {
45 adj_list[right_node].push_back(i);
48 const int sink = num_left + num_right;
49 const int source = num_left + num_right + 1;
50 for (
int i = 0; i < num_left; ++i) {
53 for (
int i = 0; i < num_right; ++i) {
57 std::vector<int> maximum_matching(num_left + num_right, -1);
59 if (max_flow.
Flow(arc) > 0) {
60 maximum_matching[max_flow.
Tail(arc)] = max_flow.
Head(arc);
61 maximum_matching[max_flow.
Head(arc)] = max_flow.
Tail(arc);
66 std::vector<bool> in_alternating_path(num_left + num_right,
false);
67 std::vector<int> to_visit;
68 for (
int i = 0; i < num_left; ++i) {
69 if (maximum_matching[i] == -1) {
70 to_visit.push_back(i);
73 while (!to_visit.empty()) {
74 const int current = to_visit.back();
76 if (in_alternating_path[current]) {
79 in_alternating_path[current] =
true;
80 for (
const int j : adj_list[current]) {
81 if (current < num_left && maximum_matching[current] != j) {
82 to_visit.push_back(j);
83 }
else if (current >= num_left && maximum_matching[current] == j) {
84 to_visit.push_back(j);
88 std::vector<bool> minimum_vertex_cover(num_left + num_right,
false);
89 for (
int i = 0; i < num_left; ++i) {
90 if (!in_alternating_path[i]) {
91 minimum_vertex_cover[i] =
true;
94 for (
int i = num_left; i < num_left + num_right; ++i) {
95 if (in_alternating_path[i]) {
96 minimum_vertex_cover[i] =
true;
99 return minimum_vertex_cover;