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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
static const double EPS = 1e-8;
typedef long long ll;

const int MAX = 501;
bool edge[MAX][MAX];
bool flow[MAX][MAX];

int N, E, Q;

bool used[500];
bool _dfs(int s, int t) {
  if (s == t)
    return true;

  used[s] = true;
  for (int i = 1; i <= N; ++i) {
    if (!used[i] && edge[s][i]) {
      if (!flow[s][i]) {
        if (_dfs(i, t)) {
          if (flow[i][s])
            flow[i][s] = false;
          else
            flow[s][i] = true;
          // flow[s][i] = true;
          return true;
        }
      } else if (flow[i][s]) {
        if (_dfs(i, t)) {
          flow[i][s] = false;
          return true;
        }
      }
    }
  }
  return false;
}
bool dfs(int s, int t) {
  fill(used, used + N + 1, false);
  return _dfs(s, t);
}
void p() {
  for (int i = 1; i <= N; ++i) {
    printf("%2d: ", i);
    for (int j = 1; j <= N; ++j)
      if (flow[i][j])
        printf("f%2d ", j);
      else if (edge[i][j])
        printf("e%2d ", j);
    printf("\\n");
  }
}
int main() {
  scanf("%d%d%d", &N, &E, &Q);
  for (int i = 0; i < E; ++i) {
    int f, t;
    scanf("%d%d", &f, &t);
    edge[f][t] = edge[t][f] = true;
  }

  int res = 0;
  while (dfs(1, N))
    ++res;

  for (int i = 0; i < Q; ++i) {
    int m, a, b;
    scanf("%d%d%d", &m, &a, &b);

    if (m == 1) {
      edge[a][b] = edge[b][a] = true;
      if (dfs(1, N))
        ++res;
    } else {
      edge[a][b] = edge[b][a] = false;

      if (flow[a][b] && !dfs(a, b)) {
        dfs(a, 1);
        dfs(N, b);
        --res;
      }
      if (flow[b][a] && !dfs(b, a)) {
        dfs(b, 1);
        dfs(N, a);
        --res;
      }
      flow[a][b] = flow[b][a] = false;
    }

    printf("%d\\n", res);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
static const double EPS = 1e-8;
typedef long long ll;

const int MAX = 501;
bool edge[MAX][MAX];
bool flow[MAX][MAX];

int N, E, Q;

bool used[500];
bool _dfs(int s, int t) {
  if (s == t)
    return true;

  used[s] = true;
  for (int i = 1; i <= N; ++i) {
    if (!used[i] && edge[s][i] && !flow[s][i] && _dfs(i, t)) {
      if (flow[i][s])
        flow[i][s] = false;
      else
        flow[s][i] = true;

      return true;
    }
  }

  return false;
}
bool dfs(int s, int t) {
  fill(used, used + N + 1, false);
  return _dfs(s, t);
}
int main() {
  scanf("%d%d%d", &N, &E, &Q);
  for (int i = 0; i < E; ++i) {
    int f, t;
    scanf("%d%d", &f, &t);
    edge[f][t] = edge[t][f] = true;
  }

  int res = 0;
  while (_dfs(1, N))
    ++res;

  for (int i = 0; i < Q; ++i) {
    int m, a, b;
    scanf("%d%d%d", &m, &a, &b);

    if (m == 1)
      edge[a][b] = edge[b][a] = true;
    else
      edge[a][b] = edge[b][a] = false;

    res = 0;
    memset(flow, 0, sizeof(flow));
    while (dfs(1, N))
      ++res;
    printf("%d\\n", res);
    // if (m == 1)
    //{
    //	edge[a][b] = edge[b][a] = true;
    //	if (dfs(1, N))
    //		++res;
    // }
    // else
    //{
    //	if (flow[a][b])
    //	{
    //		if (!dfs(1, b))
    //			--res;
    //	}
    //	else if (flow[b][a])
    //	{
    //		if (!dfs(1, a))
    //			--res;
    //	}

    //}
  }
  return 0;
}