Problem p02561 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define Int int64_t

using namespace std;

template <class Cap> struct mf_graph {
private:
  int _n;
  struct _edge {
    int to, rev;
    Cap cap;
  };
  vector<pair<int, int>> pos;
  vector<vector<_edge>> g;

public:
  mf_graph() : _n(0) {}
  mf_graph(int n) : _n(n), g(n) {}

  int add_edge(int from, int to, Cap cap) {
    assert(0 <= from && from < _n);
    assert(0 <= to && to < _n);
    assert(0 <= cap);
    int m = int(pos.size());
    pos.emplace_back(from, int(g[from].size()));
    g[from].push_back(_edge{to, int(g[to].size()), cap});
    g[to].push_back(_edge{from, int(g[from].size()) - 1, 0});
    return m;
  }

  struct edge {
    int from, to;
    Cap cap, flow;
  };

  edge get_edge(int i) {
    int m = int(pos.size());
    assert(0 <= i && i < m);
    auto _e = g[pos[i].first][pos[i].second];
    auto _re = g[_e.to][_e.rev];
    return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};
  }
  vector<edge> edges() {
    int m = int(pos.size());
    vector<edge> result;
    for (int i = 0; i < m; ++i) {
      result.push_back(get_edge(i));
    }
    return result;
  }
  void change_edge(int i, Cap new_cap, Cap new_flow) {
    int m = int(pos.size());
    assert(0 <= i && i < m);
    assert(0 <= new_flow && new_flow <= new_cap);
    auto &_e = g[pos[i].first][pos[i].second];
    auto &_re = g[_e.to][_e.rev];
    _e.cap = new_cap - new_flow;
    _re.cap = new_flow;
  }

  Cap flow(int s, int t) { return flow(s, t, numeric_limits<Cap>::max()); }
  Cap flow(int s, int t, Cap flow_limit) {
    assert(0 <= s && s < _n);
    assert(0 <= t && t < _n);

    vector<int> level(_n), iter(_n);
    deque<int> que;

    auto bfs = [&]() {
      fill(level.begin(), level.end(), -1);
      level[s] = 0;
      que.clear();
      que.push_back(s);
      while (!que.empty()) {
        int v = que.front();
        que.pop_front();
        for (auto e : g[v]) {
          if (e.cap == 0 || level[e.to] >= 0) {
            continue;
          }
          level[e.to] = level[v] + 1;
          if (e.to == t) {
            return;
          }
          que.push_back(e.to);
        }
      }
    };
    auto dfs = [&](auto self, int v, Cap up) {
      if (v == s) {
        return up;
      }
      Cap res = 0;
      int level_v = level[v];
      for (int &i = iter[v]; i < int(g[v].size()); ++i) {
        _edge &e = g[v][i];
        if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) {
          continue;
        }
        Cap d = self(self, e.to, min(up - res, g[e.to][e.rev].cap));
        if (d <= 0) {
          continue;
        }
        g[v][i].cap += d;
        g[e.to][e.rev].cap -= d;
        res += d;
        if (res == up) {
          break;
        }
      }
      return res;
    };

    Cap flow = 0;
    while (flow < flow_limit) {
      bfs();
      if (level[t] == -1) {
        break;
      }
      fill(iter.begin(), iter.end(), 0);
      while (flow < flow_limit) {
        Cap f = dfs(dfs, t, flow_limit - flow);
        if (!f) {
          break;
        }
        flow += f;
      }
    }
    return flow;
  }

  vector<bool> min_cut(int s) {
    vector<bool> visited(_n);
    queue<int> que;
    que.push(s);
    while (!que.empty()) {
      int p = que.front();
      que.pop();
      visited[p] = true;
      for (auto e : g[p]) {
        if (e.cap && !visited[e.to]) {
          visited[e.to] = true;
          que.push(e.to);
        }
      }
    }
    return visited;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<string> S(N);
  for (int i = 0; i < N; ++i) {
    cin >> S[i];
  }

  mf_graph<int> g(N * M + 2);
  int s = N * M, t = N * M + 1;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      if (S[i][j] == '#') {
        continue;
      }

      int idx = i * M + j;
      if ((i + j) % 2) {
        g.add_edge(idx, t, 1);
        continue;
      }

      g.add_edge(s, idx, 1);
      if (i > 0 && S[i - 1][j] != '#') {
        g.add_edge(idx, idx - M, 1);
      }
      if (i + 1 < N && S[i + 1][j] != '#') {
        g.add_edge(idx, idx + M, 1);
      }
      if (j > 0 && S[i][j - 1] != '#') {
        g.add_edge(idx, idx - 1, 1);
      }
      if (j + 1 < M && S[i][j + 1] != '#') {
        g.add_edge(idx, idx + 1, 1);
      }
    }
  }

  cout << g.flow(s, t) << "\\n";
  for (auto e : g.edges()) {
    if (e.from == s || e.to == t || e.flow == 0) {
      continue;
    }
    int u = e.from, v = e.to;
    if (u > v) {
      swap(u, v);
    }
    int ui = u / M, uj = u % M;
    int vi = v / M, vj = v % M;
    if (ui == vi) {
      S[ui][uj] = '>';
      S[vi][vj] = '<';
    } else {
      S[ui][uj] = 'v';
      S[vi][vj] = '^';
    }
  }
  for (auto v : S) {
    cout << v << "\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define Int int64_t

using namespace std;

template <class Cap> struct mf_graph {
private:
  int _n;
  struct _edge {
    int to, rev;
    Cap cap;
  };
  vector<pair<int, int>> pos;
  vector<vector<_edge>> g;

public:
  mf_graph() : _n(0) {}
  mf_graph(int n) : _n(n), g(n) {}

  int add_edge(int from, int to, Cap cap) {
    assert(0 <= from && from < _n);
    assert(0 <= to && to < _n);
    assert(0 <= cap);
    int m = int(pos.size());
    pos.emplace_back(from, int(g[from].size()));
    g[from].push_back(_edge{to, int(g[to].size()), cap});
    g[to].push_back(_edge{from, int(g[from].size()) - 1, 0});
    return m;
  }

  struct edge {
    int from, to;
    Cap cap, flow;
  };

  edge get_edge(int i) {
    int m = int(pos.size());
    assert(0 <= i && i < m);
    auto _e = g[pos[i].first][pos[i].second];
    auto _re = g[_e.to][_e.rev];
    return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};
  }
  vector<edge> edges() {
    int m = int(pos.size());
    vector<edge> result;
    for (int i = 0; i < m; ++i) {
      result.push_back(get_edge(i));
    }
    return result;
  }
  void change_edge(int i, Cap new_cap, Cap new_flow) {
    int m = int(pos.size());
    assert(0 <= i && i < m);
    assert(0 <= new_flow && new_flow <= new_cap);
    auto &_e = g[pos[i].first][pos[i].second];
    auto &_re = g[_e.to][_e.rev];
    _e.cap = new_cap - new_flow;
    _re.cap = new_flow;
  }

  Cap flow(int s, int t) { return flow(s, t, numeric_limits<Cap>::max()); }
  Cap flow(int s, int t, Cap flow_limit) {
    assert(0 <= s && s < _n);
    assert(0 <= t && t < _n);

    vector<int> level(_n), iter(_n);
    queue<int> que;

    auto bfs = [&]() {
      fill(level.begin(), level.end(), -1);
      level[s] = 0;
      que.push(s);
      while (!que.empty()) {
        int v = que.front();
        que.pop();
        for (auto e : g[v]) {
          if (e.cap == 0 || level[e.to] >= 0) {
            continue;
          }
          level[e.to] = level[v] + 1;
          if (e.to == t) {
            return;
          }
          que.push(e.to);
        }
      }
    };
    auto dfs = [&](auto self, int v, Cap up) {
      if (v == s) {
        return up;
      }
      Cap res = 0;
      int level_v = level[v];
      for (int &i = iter[v]; i < int(g[v].size()); ++i) {
        _edge &e = g[v][i];
        if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) {
          continue;
        }
        Cap d = self(self, e.to, min(up - res, g[e.to][e.rev].cap));
        if (d <= 0) {
          continue;
        }
        g[v][i].cap += d;
        g[e.to][e.rev].cap -= d;
        res += d;
        if (res == up) {
          break;
        }
      }
      return res;
    };

    Cap flow = 0;
    while (flow < flow_limit) {
      bfs();
      if (level[t] == -1) {
        break;
      }
      fill(iter.begin(), iter.end(), 0);
      while (flow < flow_limit) {
        Cap f = dfs(dfs, t, flow_limit - flow);
        if (!f) {
          break;
        }
        flow += f;
      }
    }
    return flow;
  }

  vector<bool> min_cut(int s) {
    vector<bool> visited(_n);
    queue<int> que;
    que.push(s);
    while (!que.empty()) {
      int p = que.front();
      que.pop();
      visited[p] = true;
      for (auto e : g[p]) {
        if (e.cap && !visited[e.to]) {
          visited[e.to] = true;
          que.push(e.to);
        }
      }
    }
    return visited;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<string> S(N);
  for (int i = 0; i < N; ++i) {
    cin >> S[i];
  }

  int n = N * M + 2;
  mf_graph<int> g(n);
  int s = N * M, t = N * M + 1;
  int dx[4] = {0, 0, 1, -1};
  int dy[4] = {1, -1, 0, 0};
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      if (S[i][j] == '#') {
        continue;
      }

      int idx = i * M + j;
      if ((i + j) % 2 == 1) {
        g.add_edge(idx, t, 1);
        continue;
      }

      g.add_edge(s, idx, 1);
      for (int k = 0; k < 4; ++k) {
        int ni = i + dx[k], nj = j + dy[k];
        if (ni < 0 || ni >= N || nj < 0 || nj >= M) {
          continue;
        }
        if (S[ni][nj] == '#') {
          continue;
        }
        g.add_edge(idx, ni * M + nj, 1);
      }
    }
  }

  cout << g.flow(s, t) << "\\n";
  for (auto e : g.edges()) {
    if (e.from == s || e.to == t || e.flow == 0) {
      continue;
    }
    int xi = e.from / M, xj = e.from % M;
    int yi = e.to / M, yj = e.to % M;
    if (xi == yi) {
      S[xi][xj] = '>';
      S[yi][yj] = '<';
      if (xj > yj) {
        swap(S[xi][xj], S[yi][yj]);
      }
    } else {
      S[xi][xj] = 'v';
      S[yi][yj] = '^';
      if (xj > yj) {
        swap(S[xi][xj], S[yi][yj]);
      }
    }
  }
  for (auto v : S) {
    cout << v << "\\n";
  }

  return 0;
}