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

#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int Ni = 300;
const int MAX = 1 << 26;
struct Edge {
  int u, v, c;
  int next;
} edge[4000];
int n, m;
int edn;   //\\u8fb9\\u6570
int p[Ni]; //\\u7236\\u4eb2
int d[Ni];
int sp, tp; //\\u539f\\u70b9\\uff0c\\u6c47\\u70b9

void addedge(int u, int v, int c) {
  edge[edn].u = u;
  edge[edn].v = v;
  edge[edn].c = c;
  edge[edn].next = p[u];
  p[u] = edn++;

  edge[edn].u = v;
  edge[edn].v = u;
  edge[edn].c = 0;
  edge[edn].next = p[v];
  p[v] = edn++;
}
int bfs() {
  queue<int> q;
  memset(d, -1, sizeof(d));
  d[sp] = 0;
  q.push(sp);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (int i = p[cur]; i != -1; i = edge[i].next) {
      int u = edge[i].v;
      if (d[u] == -1 && edge[i].c > 0) {
        d[u] = d[cur] + 1;
        q.push(u);
      }
    }
  }
  return d[tp] != -1;
}
int dfs(int a, int b) {
  int r = 0;
  if (a == tp)
    return b;
  for (int i = p[a]; i != -1 && r < b; i = edge[i].next) {
    int u = edge[i].v;
    if (edge[i].c > 0 && d[u] == d[a] + 1) {
      int x = min(edge[i].c, b - r);
      x = dfs(u, x);
      r += x;
      edge[i].c -= x;
      edge[i ^ 1].c += x;
    }
  }
  if (!r)
    d[a] = -2;
  return r;
}

int dinic(int sp, int tp) {
  int total = 0, t;
  while (bfs()) {
    while (t = dfs(sp, MAX))
      total += t;
  }
  return total;
}
int main() {
  scanf("%d%d", &n, &m);
  edn = 0;
  memset(p, -1, sizeof(p));
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    addedge(a - 1, b - 1, 1);
    addedge(b - 1, a - 1, 1);
  }
  scanf("%d%d", &sp, &tp);
  sp--;
  tp--;
  int ans = dinic(sp, tp), tot = 0;
  for (int i = 0; i < m; i++)
    if (edge[i * 4 + 2].c == 0)
      tot++;
  printf("%d\\n%d\\n", ans, tot);
  for (int i = 0; i < m; i++)
    if (edge[i * 4 + 2].c == 0)
      printf("%d\\n", i + 1);
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
struct edge {
  int s, f, c, r;
} e[2000];
int n, m, top, sp, fp, vis[1000];
vector<int> g[300];
int DFS(int v) {
  if (v == fp)
    return 1;
  int sz = g[v].size();
  vis[v] = 1;
  for (int i = 0; i < sz; i++) {
    edge ed = e[g[v][i]];
    if (ed.s == v) {
      int u = ed.f;
      if (vis[u] || ed.c == 0)
        continue;
      if (DFS(u)) {
        e[g[v][i]].c = 0;
        return 1;
      }
    } else {
      int u = ed.s;
      if (vis[u] || ed.c == 1)
        continue;
      if (DFS(u)) {
        e[g[v][i]].c = 1;
        return 1;
      }
    }
  }
  vis[v] = 0;
  return 0;
}
int solve() {
  int ans = 0;
  memset(vis, 0, sizeof(vis));
  while (DFS(sp)) {
    ans++;
    memset(vis, 0, sizeof(vis));
  }
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    e[top].s = a - 1;
    e[top].f = b - 1;
    e[top].c = 1;
    g[a - 1].push_back(top);
    g[b - 1].push_back(top++);
    e[top].s = b - 1;
    e[top].f = a - 1;
    e[top].c = 1;
    g[a - 1].push_back(top);
    g[b - 1].push_back(top++);
  }
  scanf("%d%d", &sp, &fp);
  sp--;
  fp--;
  int ans = solve(), tot = 0;
  for (int i = 0; i < m; i++)
    if (e[i * 2 + 1].c == 0)
      tot++;
  printf("%d\\n%d\\n", ans, tot);
  for (int i = 0; i < m; i++)
    if (e[i * 2 + 1].c == 0)
      printf("%d\\n", i + 1);
}