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

// なんかTLEなるんですが公開されているテストケースはすべて一瞬で計算してくれることを確認したので出します
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

struct edge {
  int to;
  int cap;
  int rev;
  edge() {}
  edge(int t, int c, int r) : to(t), cap(c), rev(r) {}
};
const int MAX_V = 500;

vector<edge> G[MAX_V];
bool used[MAX_V];

void add_edge(int from, int to, int cap) {
  G[from].push_back(edge(to, cap, G[to].size()));
  G[to].push_back(edge(from, 0, G[from].size() - 1));
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int 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;
}

int max_flow(int s, int t) {
  int flow = 0;
  for (;;) {
    for (int i = 0; i < MAX_V; i++)
      used[i] = false;
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

int main(void) {
  int X, Y, E;
  cin >> X >> Y >> E;
  int s = X + Y, t = s + 1;
  for (int i = 0; i < E; i++) {
    int x, y;
    cin >> x >> y;
    add_edge(x, y + X, 1);
  }
  for (int i = 0; i < X; i++) {
    add_edge(s, i, 1);
  }
  for (int i = 0; i < Y; i++) {
    add_edge(i + X, t, 1);
  }
  cout << max_flow(s, t) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

struct edge {
  int to;
  int cap;
  int rev;
  edge() {}
  edge(int t, int c, int r) : to(t), cap(c), rev(r) {}
};
const int MAX_V = 500;

vector<edge> G[MAX_V];
bool used[MAX_V];

void add_edge(int from, int to, int cap) {
  G[from].push_back(edge(to, cap, G[to].size()));
  G[to].push_back(edge(from, 0, G[from].size() - 1));
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
}

int max_flow(int s, int t) {
  int flow = 0;
  for (;;) {
    for (int i = 0; i < MAX_V; i++)
      used[i] = false;
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

int main(void) {
  int X, Y, E;
  cin >> X >> Y >> E;
  int s = X + Y, t = s + 1;
  for (int i = 0; i < E; i++) {
    int x, y;
    cin >> x >> y;
    add_edge(x, y + X, 1);
  }
  for (int i = 0; i < X; i++) {
    add_edge(s, i, 1);
  }
  for (int i = 0; i < Y; i++) {
    add_edge(i + X, t, 1);
  }
  cout << max_flow(s, t) << endl;
  return 0;
}