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

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define sar(a, n)                                                              \\
  cout << #a << ":";                                                           \\
  rep(kbrni, n) cout << " " << a[kbrni];                                       \\
  cout << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define svecp(v)                                                               \\
  cout << #v << ":";                                                           \\
  each(kbrni, v) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 1005;

struct edge {
  int to;
  int cap;
  int rev;
};

template <typename T> class Dinic {
public:
  vector<vector<edge>> G;
  vector<int> level, iter;
  int n;
  Dinic(int node_size) {
    n = node_size;
    G.resize(node_size), level.resize(node_size), iter.resize(node_size);
  }
  // 辺を張る
  int add_edge(int from, int to, T cap) {
    G[from].push_back((edge){to, cap, (int)G[to].size()});
    G[to].push_back((edge){from, (T)0, (int)G[from].size() - 1});
    return len(G[from]) - 1;
  }
  void bfs(int s) {
    fill(level.begin(), level.end(), -1);
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      rep(i, G[v].size()) {
        edge &e = G[v][i];
        if (e.cap > 0 && level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          que.push(e.to);
        }
      }
    }
  }
  T dfs(int v, int t, T f) {
    if (v == t) {
      return f;
    }
    for (int &i = iter[v]; i < (int)G[v].size(); i++) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[v] < level[e.to]) {
        T d = dfs(e.to, t, min(f, e.cap));
        if (d > 0) {
          e.cap -= d;
          G[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }
  // 最大流を計算
  T max_flow(int s, int t) {
    T flow = 0;
    for (;;) {
      bfs(s);
      if (level[t] < 0) {
        return flow;
      }
      fill(iter.begin(), iter.end(), 0);
      T f;
      while ((f = dfs(s, t, numeric_limits<T>::max())) > 0) {
        flow += f;
      }
    }
  }
};

int d1[MAX_N], d2[MAX_N];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (1) {
    int n, m, s, t;
    cin >> n >> m >> s >> t;
    if (n == 0) {
      break;
    }
    --s, --t;
    Dinic<int> dn(n);
    vp vec(m);
    rep(i, m) {
      int x, y;
      cin >> x >> y;
      --x, --y;
      y = dn.add_edge(x, y, 1);
      vec[i] = P(x, y);
    }
    int res = dn.max_flow(s, t);
    vector<vector<edge>> &G = dn.G;
    rep(i, n) { d1[i] = d2[i] = INF; }
    queue<int> q1, q2;
    q1.push(s);
    d1[s] = 0;
    while (!q1.empty()) {
      int p = q1.front();
      q1.pop();
      each(e, G[p]) {
        int v = e.to;
        int c = e.cap;
        if (c > 0 && d1[v] > d1[p] + 1) {
          d1[v] = d1[p] + 1;
          q1.push(v);
        }
      }
    }
    q2.push(t);
    d2[t] = 0;
    while (!q2.empty()) {
      int p = q2.front();
      q2.pop();
      each(e, G[p]) {
        int v = e.to;
        int r = e.rev;
        int c = G[v][r].cap;
        if (c > 0 && d2[v] > d2[p] + 1) {
          d2[v] = d2[p] + 1;
          q2.push(v);
        }
      }
    }
    int cnt = 0;
    rep(i, m) {
      int a = vec[i].fi, b = vec[i].se;
      if (G[a][b].cap == 1) {
        if (d1[G[a][b].to] != INF && d2[a] != INF) {
          cnt++;
        }
      }
    }
    if (cnt) {
      cout << res + 1 << " " << cnt << "\\n";
    } else {
      cout << res << " 0\\n";
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define sar(a, n)                                                              \\
  cout << #a << ":";                                                           \\
  rep(kbrni, n) cout << " " << a[kbrni];                                       \\
  cout << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define svecp(v)                                                               \\
  cout << #v << ":";                                                           \\
  each(kbrni, v) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 1005;

bool used[MAX_N];

template <typename T> class FordFulkerson {
public:
  vvi G;
  int V;
  FordFulkerson(int node_size) {
    V = node_size;
    G.resize(V, vi(V, 0));
  }
  void add_edge(int from, int to, int cap) { G[from][to] += cap; }
  int dfs(int v, int t, int f) {
    if (v == t)
      return f;
    used[v] = true;
    rep(i, V) {
      int &cap = G[v][i];
      if (!used[i] && cap > 0) {
        int d = dfs(i, t, min(f, cap));
        if (d > 0) {
          cap -= d;
          G[i][v] += d;
          return d;
        }
      }
    }
    return 0;
  }
  int max_flow(int s, int t) {
    int flow = 0;
    while (true) {
      memset(used, 0, sizeof(used));
      int f = dfs(s, t, INF);
      if (f == 0)
        return flow;
      flow += f;
    }
  }
  int update(int s, int t, int f) {
    memset(used, 0, sizeof(used));
    return dfs(s, t, f);
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (1) {
    int n, m, s, t;
    cin >> n >> m >> s >> t;
    --s, --t;
    if (n == 0) {
      break;
    }
    FordFulkerson<int> ff(n);
    vp path(m);
    rep(i, m) {
      int a, b;
      cin >> a >> b;
      --a, --b;
      ff.add_edge(a, b, 1);
      path[i] = P(a, b);
    }
    int res = ff.max_flow(s, t);
    int ans = res;
    int ans2 = -1;
    vvi G = ff.G;
    rep(i, m) {
      int u = path[i].fi, v = path[i].se;
      if (G[v][u] == 0) {
        G[u][v]--;
        int k = ff.update(s, v, 1);
        int l = ff.update(u, t, 1);
        if (min(k, l) == 1) {
          ans = res + 1;
          ans2 = i;
          break;
        }
        if (k)
          ff.update(v, s, 1);
        if (l)
          ff.update(t, u, 1);
        G[u][v]++;
      }
    }
    if (ans == res) {
      cout << ans << " "
           << "0\\n";
    } else {
      cout << ans << " " << ans2 + 1 << "\\n";
    }
  }
  return 0;
}