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

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

const int INF = 1000000;
int cap[500][500];

int maximumFlow(int n, int s, int t, bool first = false) {
  int res = 0;
  int prev[500];
  while (true) {
    queue<int> qu;
    qu.push(s);
    memset(prev, -1, sizeof(prev));
    prev[s] = s;
    while (!qu.empty() && prev[t] < 0) {
      int u = qu.front();
      qu.pop();
      for (int i = 0; i < n; i++) {
        if (prev[i] < 0 && cap[u][i] > 0) {
          prev[i] = u;
          qu.push(i);
        }
      }
    }
    if (prev[t] < 0)
      return res;
    for (int j = t; prev[j] != j; j = prev[j])
      cap[prev[j]][j]--, cap[j][prev[j]]++;
    res++;
    if (!first)
      return res;
  }
  return 0;
}

int main() {
  int N, E, Q;
  while (cin >> N >> E >> Q) {
    memset(cap, 0, sizeof(cap));
    for (int i = 0; i < E; i++) {
      int F, T;
      scanf("%d %d", &F, &T);
      cap[F - 1][T - 1] = 1;
      cap[T - 1][F - 1] = 1;
    }
    int cur = maximumFlow(N, 0, N - 1, true);
    for (int i = 0; i < Q; i++) {
      int M, A, B;
      scanf("%d %d %d", &M, &A, &B);
      if (M == 1) {
        cap[A - 1][B - 1] = 1;
        cap[B - 1][A - 1] = 1;
        cur += maximumFlow(N, 0, N - 1);
      }
      if (M == 2) {
        int s = A - 1, t = B - 1;
        for (int k = 0; k < 2; k++) {
          if (cap[t][s] == 2) {
            cap[t][s] = 0;
            int tmp = maximumFlow(N, s, t);
            if (tmp == 0) {
              maximumFlow(N, s, 0);
              maximumFlow(N, N - 1, t);
              cur--;
            }
          }
          swap(s, t);
        }
        cap[s][t] = cap[t][s] = 0;
      }
      cout << cur << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

const int INF = 1000000;
int cap[500][500];

int maximumFlow(int n, int s, int t, bool first = false) {
  int res = 0;
  while (true) {
    queue<int> qu;
    qu.push(s);
    vector<int> prev(n, -1);
    prev[s] = s;
    while (!qu.empty() && prev[t] < 0) {
      int u = qu.front();
      qu.pop();
      for (int i = 0; i < n; i++) {
        if (prev[i] < 0 && cap[u][i] > 0) {
          prev[i] = u;
          qu.push(i);
        }
      }
    }
    if (prev[t] < 0)
      return res;
    int inc = INF;
    for (int j = t; prev[j] != j; j = prev[j])
      inc = min(inc, cap[prev[j]][j]);
    for (int j = t; prev[j] != j; j = prev[j])
      cap[prev[j]][j] -= inc, cap[j][prev[j]] += inc;
    res += inc;
    if (!first)
      return res;
  }
  return 0;
}
int main() {
  int N, E, Q;
  while (cin >> N >> E >> Q) {
    memset(cap, 0, sizeof(cap));
    for (int i = 0; i < E; i++) {
      int F, T;
      scanf("%d %d", &F, &T);
      cap[F - 1][T - 1] = 1;
      cap[T - 1][F - 1] = 1;
    }
    int cur = maximumFlow(N, 0, N - 1, true);
    for (int i = 0; i < Q; i++) {
      int M, A, B;
      scanf("%d %d %d", &M, &A, &B);
      if (M == 1) {
        cap[A - 1][B - 1] = 1;
        cap[B - 1][A - 1] = 1;
        cur += maximumFlow(N, 0, N - 1);
      }
      if (M == 2) {
        int s = A - 1, t = B - 1;
        for (int k = 0; k < 2; k++) {
          if (cap[t][s] == 2) {
            cap[t][s] = 0;
            int tmp = maximumFlow(N, s, t);
            if (tmp == 0) {
              int c = t;
              while (c != N - 1) {
                for (int j = 0; j < N; j++) {
                  if (cap[j][c] == 2) {
                    cap[j][c] = cap[c][j] = 1, c = j;
                    break;
                  }
                }
              }
              c = s;
              while (c != 0) {
                for (int j = 0; j < N; j++) {
                  if (cap[c][j] == 2) {
                    cap[c][j] = cap[j][c] = 1, c = j;
                    break;
                  }
                }
              }
              cur--;
            }
          }
          swap(s, t);
        }
        cap[s][t] = cap[t][s] = 0;
        /*
        if(cap[A-1][B-1] == 0){
                cap[B-1][A-1] = 0;
                int tmp = maximumFlow(N, A-1, B-1);
                if(tmp==0){
                        int c = B-1;
                        while(c!=N-1){
                                for(int j=0;j<N;j++){
                                        if(cap[j][c] == 2){
                                                cap[j][c] = cap[c][j] = 1, c =
        j; break;
                                        }
                                }
                        }
                        c = A-1;
                        while(c!=0){
                                for(int j=0;j<N;j++){
                                        if(cap[c][j] == 2){
                                                cap[c][j] = cap[j][c] = 1, c =
        j; break;
                                        }
                                }
                        }
                        cur--;
                }
        }
        else if(cap[B-1][A-1] == 0){
                cap[A-1][B-1] = 0;
                int tmp = maximumFlow(N, B-1, A-1);
                if(tmp==0){
                        int c = A-1;
                        while(c!=N-1){
                                for(int j=0;j<N;j++){
                                        if(cap[j][c] == 2){
                                                cap[j][c] = cap[c][j] = 1, c =
        j; break;
                                        }
                                }
                        }
                        c = B-1;
                        while(c!=0){
                                for(int j=0;j<N;j++){
                                        if(cap[c][j] == 2){
                                                cap[c][j] = cap[j][c] = 1, c =
        j; break;
                                        }
                                }
                        }
                        cur--;
                }
        }
        else {
                cap[A-1][B-1] = 0;
                cap[B-1][A-1] = 0;
        }
        */
      }
      cout << cur << endl;
    }
  }
}