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

#include <limits.h>
#include <stdio.h>
#include <string.h>
#define oo INT_MAX

struct edge {
  int v, g, next;
} a[400000];
int H[400], d[400], C[400], N, M, u, v, c, cnt, ans, S, T, k, i, j;

int min(int x, int y) { return x < y ? x : y; }

int add(int u, int v, int c) {
  a[++cnt].v = v;
  a[cnt].g = c;
  a[cnt].next = H[u];
  H[u] = cnt;
}

int build(int u, int v, int c) {
  add(u, v, c);
  add(v, u, 0);
}

int sap(int u, int flow) {
  int f = 0, tt, delta;
  if (u == T)
    return flow;
  for (tt = H[u]; tt; tt = a[tt].next) {
    v = a[tt].v;
    if (a[tt].g && d[u] == d[v] + 1) {
      delta = sap(v, min(flow, a[tt].g));
      a[tt].g -= delta;
      a[tt + (tt & 1 ? 1 : -1)].g += delta;
      f += delta;
      flow -= delta;
      if (!flow)
        break;
    }
  }
  if (!f) {
    --C[d[u]];
    if (!C[d[u]])
      d[S] = N + 2;
    ++C[++d[u]];
  }
  return f;
}

int main() {
  while (~scanf("%d%d", &N, &M)) {
    memset(H, 0, sizeof(H));
    memset(d, 0, sizeof(d));
    memset(C, 0, sizeof(C));
    cnt = 0;
    C[0] = N;
    for (i = 1; i <= M; i++) {
      scanf("%d%d", &u, &v);
      build(u, v, 1);
      build(v, u, 1);
    }
    scanf("%d%d", &S, &T);
    ans = 0;
    while (d[S] <= N + 1)
      ans += sap(S, oo);
    printf("%d\\n", ans);
    ans = 0;
    for (i = 1; i <= M; i++)
      ans += a[4 * i - 3].g;
    printf("%d\\n", ans);
    for (i = 1; i <= M; i++)
      if (a[4 * i - 3].g)
        printf("%d\\n", i);
  }
  return 0;
}

Error: TLE

Fixed submission:

/*
 * Author:  xioumu
 * Created Time:  2012/10/8 14:10:09
 * File Name: E.cpp
 * solve: E.cpp
 */
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define sz(v) ((int)(v).size())
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
#define repd(i, a, b) for (int i = (a); i >= (b); --i)
#define clr(x) memset(x, 0, sizeof(x))
#define clrs(x, y) memset(x, y, sizeof(x))
#define out(x) printf(#x " %d\\n", x)
typedef long long lint;
const int maxint = -1u >> 1;
const double esp = 1e-8;
#define inf 10000000
#define maxn 400
#define maxm 2000000
int e[maxn][maxn];
int n, m, ans, S, T;
int dui[maxm];
int d[maxn];
bool bj[maxn];
int x[2000], y[2000];
vector<int> ret;
int dfs(
    int t,
    int v) { // dfs謇セ霑樒サュ蠅槫ケソ霍ッ蠕?	if (t==T || v==0) return v;
  int ans = 0, flow;
  for (int i = 1; i <= n; i++) {
    if (e[t][i] > 0 && d[i] == d[t] + 1) {
      flow = dfs(i, min(v, e[t][i])); // 騾貞ス貞セ怜芦荳矩擇逧?庄謾ケ霑幃?
      e[t][i] -= flow;
      e[i][t] += flow; // 菫ョ謾ケ隸・霎ケ豬??
      v -= flow;
      ans += flow; // 菫ョ謾ケ髦亥? 譖エ譁ー豬??
      if (v == 0)
        break;
    }
  }
  return ans;
}
bool bfs(int s) {
  int head, last, x;
  memset(d, 0, sizeof(d));
  memset(bj, false, sizeof(bj));
  d[s] = 1;
  head = 0;
  last = 1;
  dui[last] = s;
  bj[s] = true;
  while (head < last) {
    x = dui[++head];
    for (int i = 1; i <= n; i++) {
      if (!bj[i] && e[x][i] > 0) {
        bj[i] = true;
        d[i] = d[x] + 1;
        dui[++last] = i;
      }
    }
  }
  return bj[T];
}
void clrr() {
  ans = 0;
  clr(e);
  ret.clear();
}
int main() {
  while (scanf("%d%d", &n, &m) == 2) {
    clrr();
    // cout<<n<<m<<endl;
    repf(i, 1, m) {
      scanf("%d%d", &x[i], &y[i]);
      if (e[x[i]][y[i]] == 1)
        while (1)
          ;
      e[x[i]][y[i]] = e[y[i]][x[i]] = 1;
    }
    repf(i, 1, m) repf(j, 1, m) {
      if (i == j)
        continue;
      if (x[i] == x[j] && y[i] == y[j])
        while (1)
          ;
      if (x[i] == y[j] && x[j] == y[i])
        while (1)
          ;
    }
    // repf( i , 1 , n ) {
    // repf( j , 1 , n )
    // cout<<e[i][j]<<" ";
    // cout<<endl;
    //}
    scanf("%d%d", &S, &T);
    while (bfs(S))
      ans += dfs(S, inf);
    // cout<<"------------------------------------------------"<<endl;
    printf("%d\\n", ans);
    repf(i, 1, m) if (e[y[i]][x[i]] == 0) ret.push_back(i);
    printf("%d\\n", ret.size());
    if (ret.size() != 0) {
      rep(i, ret.size() - 1) printf("%d\\n", ret[i]);
      printf("%d\\n", ret[ret.size() - 1]);
    }
    // cout<<"-------------------------------------------------"<<endl;
  }
}