Problem p01430 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>

using namespace std;

// #define RED_FILE

#ifdef RED_FILE
ifstream ifs("input.txt");
ofstream ofs("output.txt");
#define cin ifs
#define cout ofs
#endif

const int MAX_V = 1000;
const int INF = 1000000000;
const int MAX_E = 200000;

struct edge {
  int to, cap, rev, id;
  int flow;
  edge(int to_, int cap_, int rev_, int id_) {
    to = to_;
    cap = cap_;
    rev = rev_;
    id = id_;
    flow = 0;
  }
};

vector<edge> G[MAX_V];
bool used[MAX_V];
// Ü¾¶«Äéü©Ç¤©
bool isUsable[MAX_E];
// idxðßé
int lineIdx[MAX_V][MAX_V];

// edgeðÇÁ
void add_edge(int from, int to, int cap, int id) {
  G[from].push_back(edge(to, cap, G[to].size(), id));
  G[to].push_back(edge(from, cap, G[from].size() - 1, id));
}

// ÁpXðTõ
int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < (int)G[v].size(); i++) {
    edge &e = G[v][i];
    // ØfÏÝ
    if (!isUsable[e.id])
      continue;
    // Ü¾t[ð¬¹éÈçÎA¬·
    if (!used[e.to] && e.flow < e.cap) {
      // ¡ñÌ¬Ê
      int quan = e.cap - e.flow;
      // if(e.flow<0)quan=-(e.flow);
      int d = dfs(e.to, t, min(f, quan));
      if (d > 0) {
        e.flow += d;
        // ½ÎÉÍ}CiXÌvfðüêÄ¨­
        G[e.to][e.rev].flow = -e.flow;
        return d;
      }
    }
  }
  return 0;
} // ¸­pXðTõ
int dfs2(int v, int t) {
  if (v == t)
    return 1;
  used[v] = true;
  for (int i = 0; i < (int)G[v].size(); i++) {
    edge &e = G[v][i];
    if (!isUsable[e.id])
      continue;
    // 0æè¬³¯êÎAß·
    if (!used[e.to] && e.flow < 0) {
      int d = dfs2(e.to, t);
      if (d > 0) {
        e.flow += 1;
        G[e.to][e.rev].flow = -e.flow;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t) {
  int flow = 0;
  while (1) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    else
      flow += f;
  }
  return 0;
}

int main() {
  int n, ee, q;
  cin >> n >> ee >> q;
  for (int i = 0; i < ee; i++) {
    int f, t;
    cin >> f >> t;
    // Æè ¦¸ÇÁ
    add_edge(f, t, 1, i);
    isUsable[i] = true;
    lineIdx[f][t] = lineIdx[t][f] = i;
  }
  // »ÝÌÅåflowðßé
  int cflow = max_flow(1, n);
  for (int j = 0; j < q; j++) {
    int m, a, b;
    cin >> m >> a >> b;
    if (m == 1) {
      add_edge(a, b, 1, ee);
      lineIdx[a][b] = lineIdx[b][a] = ee;
      isUsable[ee] = true;
      ee++;
      cflow += max_flow(1, n);
    } else {
      // Ú±ªØê½Æ«AØê½êÉt[ª¬êÄ¢é©»¤ÅÈ¢©Åêª¯
      int eidx = lineIdx[a][b];
      for (int i = 0; i < (int)G[a].size(); i++) {
        edge &e = G[a][i];
        if (!isUsable[e.id])
          continue;
        if (eidx == e.id) {
          // ¬êÄ¢È¢ê,tOðÜé¾¯
          if (e.flow == 0)
            isUsable[eidx] = false;
          // ¬êÄ¢éê
          else {
            int from = a;
            int to = b;
            if (e.flow > 0)
              swap(from, to);
            // flowð0Éú»
            e.flow = G[e.to][e.rev].flow = 0;
            // ûüÉµ½ªÁÄtü«Éflowð¬·
            isUsable[eidx] = false;
            memset(used, 0, sizeof(used));
            int num = dfs(to, from, 1);
            // ¬¹½ê
            if (num == 1)
              break;
            // ¬¹È©Á½ê,ß·
            // dfs2(from,to);
            memset(used, 0, sizeof(used));
            dfs2(n, from);
            memset(used, 0, sizeof(used));
            dfs2(to, 1);
            // Øf
            cflow--;
            cflow += max_flow(1, n);
          }
          break;
        }
      }
    }
    cout << cflow << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>

using namespace std;

#define RED_FILE

#ifdef RED_FILE
ifstream ifs("input.txt");
ofstream ofs("output.txt");
#define cin ifs
#define cout ofs
#endif

struct edge {
  int to, cap, rev;
  int id;
  edge(int to_, int cap_, int rev_, int id_) {
    to = to_;
    cap = cap_;
    rev = rev_;
    id = id_;
  }
};

const int MAX_V = 1000;
const int INF = 1000000000;
const int MAX_E = 20000;

vector<edge> G[MAX_V];
bool used[MAX_V];
// ûüªÜÁ½©Ç¤©
bool isDirected[2 * MAX_E];
// »ÌûüªÇ¿ç©
int fromNode[2 * MAX_E];
// Ü¾¶«Äéü©Ç¤©
bool isUsable[2 * MAX_E];
// idxðßé
int lineIdx[MAX_V][MAX_V];

// edgeðÇÁ
void add_edge(int from, int to, int cap, int id) {
  G[from].push_back(edge(to, cap, G[to].size(), id));
  G[to].push_back(edge(from, 0, G[from].size() - 1, id));
}

// ÁpXðTõ
int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < (int)G[v].size(); i++) {
    edge &e = G[v][i];
    // Ü¾ØçêÄ¢È¢©ÂgíêÄ¢È¢m[hÖÌJÚ
    if (!used[e.to] && isUsable[e.id]) {
      bool ok = false;
      // Ü¾ûüª¢è
      if (!isDirected[e.id]) {
        isDirected[e.id] = true;
        fromNode[e.id] = v;
        // eÊðfromÌûüÉ½­í¦é
        if (e.cap == 0)
          swap(e.cap, G[e.to][e.rev].cap);
        // ¡ñÏXðsÁ½
        ok = true;
      }
      // eÊª0æèå«¢Æ«
      if (e.cap > 0) {
        int d = dfs(e.to, t, min(f, e.cap));
        // 1Ìt[ª¬¹½ê
        if (d > 0) {
          e.cap -= d;
          G[e.to][e.rev].cap += d;
          // eÊªÅÌóÔÉàÇÁ½çADirectedtOðð
          if (fromNode[e.id] == e.to)
            if (e.cap == 0)
              isDirected[e.id] = false;
          // dÌflowð¬·
          return d;
        } else {
          // t[ª¬¹È¢ÈçA¡ñèµ½ûüð³ø
          if (ok)
            isDirected[e.id] = false;
        }
      }
    }
  }
  return 0;
}

int max_flow(int s, int t) {
  int flow = 0;
  while (1) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    else
      flow += f;
  }
  return 0;
}

int main() {
  int n, e, q;
  cin >> n >> e >> q;
  for (int i = 0; i < e; i++) {
    int f, t;
    cin >> f >> t;
    // Æè ¦¸ÇÁ
    add_edge(f, t, 1, i);
    isUsable[i] = true;
    lineIdx[f][t] = lineIdx[t][f] = i;
  }
  // »ÝÌÅåflowðßé
  int cflow = max_flow(1, n);
  for (int j = 0; j < q; j++) {
    int m, a, b;
    cin >> m >> a >> b;
    if (m == 1) {
      add_edge(a, b, 1, e);
      lineIdx[a][b] = lineIdx[b][a] = e;
      isUsable[e] = true;
      e++;
      cflow += max_flow(1, n);
    } else {
      // Ú±ªØê½Æ«AØê½êÉt[ª¬êÄ¢é©»¤ÅÈ¢©Åêª¯
      int eidx = lineIdx[a][b];
      // Øf
      isUsable[eidx] = false;
      if (isDirected[eidx]) {
        for (int i = 0; i < (int)G[a].size(); i++) {
          // ­©
          if (eidx == G[a][i].id) {
            if (fromNode[eidx] == a) {
              // 1Ìflowð¬µÄµß·
              memset(used, 0, sizeof(used));
              dfs(n, b, 1);
              memset(used, 0, sizeof(used));
              dfs(a, 1, 1);
              cflow--;
              // ¬¹éÈç¬·
              cflow += max_flow(1, n);
            } else {
              // ûüÍÜÁÄ¢éªAt[ÉÍÖWµÈ¢
              memset(used, 0, sizeof(used));
              dfs(n, a, 1);
              memset(used, 0, sizeof(used));
              dfs(b, 1, 1);
              cflow--;
              // ¬¹éÈç¬·
              cflow += max_flow(1, n);
            }
            break;
          }
        }
      }
    }
    cout << cflow << endl;
  }
  return 0;
}