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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))
#define iter(v, ite) for (auto ite = (v).begin(); ite != (v).end(); ite++)
#define mimunum(v, x)                                                          \\
  distance((v).begin(), lower_bound((v).begin(), (v).end(), x))

int n, m, qn;

int vs[505][505] = {};
int gone[505];

bool dfs(int no, int gl) {
  if (no == gl)
    return true;
  gone[no] = 1;
  reg(to, 1, n) {
    if (gone[to])
      continue;
    if (vs[no][to] > 0) {
      if (dfs(to, gl)) {
        vs[no][to]--;
        vs[to][no]++;
        return true;
      }
    }
  }
  return false;
}

int main(void) {
  scanf("%d%d%d", &n, &m, &qn);
  rep(i, m) {
    int a, b;
    scanf("%d%d", &a, &b);
    vs[a][b] = vs[b][a] = 1;
  }

  int nf = 0;
  for (;;) {
    memset(gone, 0, sizeof(gone));
    if (dfs(1, n))
      nf++;
    else
      break;
  }
  rep(q, qn) {
    int t, a, b;
    scanf("%d%d%d", &t, &a, &b);
    if (t == 1) {
      vs[a][b] = vs[b][a] = 1;
      memset(gone, 0, sizeof(gone));
      if (dfs(1, n))
        nf++;
    } else {
      int fr = -1, to;
      if (vs[a][b] == 0) {
        fr = a;
        to = b;
      } else if (vs[b][a] == 0) {
        fr = b;
        to = a;
      }
      vs[a][b] = vs[b][a] = 0;
      if (fr >= 0) {
        memset(gone, 0, sizeof(gone));
        if (!dfs(fr, to)) {
          bool ok = true;
          memset(gone, 0, sizeof(gone));
          ok &= dfs(n, to);
          memset(gone, 0, sizeof(gone));
          ok &= dfs(fr, 1);
          nf--;
        }
        memset(gone, 0, sizeof(gone));
        if (dfs(1, n))
          nf++;
      }
      /*
      if(fr>=0){
              bool ok=true;
              memset(gone,0,sizeof(gone));
              ok &= dfs(n,to);
              memset(gone,0,sizeof(gone));
              ok &= dfs(fr,1);
              if(!ok){
                      printf("%d (%d %d %d)\\n",q,t,a,b);
                      reg(i,1,n){
                              reg(j,1,n)printf("%d%c",vs[i][j],j==n?'\\n':' ');
                      }
                      if(!ok)return -1;
              }
              nf--;
              memset(gone,0,sizeof(gone));
              if(dfs(1,n))nf++;
      }
      */
    }
    printf("%d\\n", nf);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))
#define iter(v, ite) for (auto ite = (v).begin(); ite != (v).end(); ite++)
#define mimunum(v, x)                                                          \\
  distance((v).begin(), lower_bound((v).begin(), (v).end(), x))

int n, m, qn;

int vs[505][505] = {};
int gone[505];

bool dfs(int no, int gl) {
  if (no == gl)
    return true;
  gone[no] = 1;
  reg(to, 1, n) {
    if (gone[to])
      continue;
    if (vs[no][to] > 0) {
      if (dfs(to, gl)) {
        vs[no][to]--;
        vs[to][no]++;
        return true;
      }
    }
  }
  return false;
}

int main(void) {
  scanf("%d%d%d", &n, &m, &qn);
  rep(i, m) {
    int a, b;
    scanf("%d%d", &a, &b);
    vs[a][b] = vs[b][a] = 1;
  }

  int nf = 0;
  for (;;) {
    memset(gone, 0, sizeof(gone));
    if (dfs(1, n))
      nf++;
    else
      break;
  }
  rep(i, qn) {
    int t, a, b;
    scanf("%d%d%d", &t, &a, &b);
    if (t == 1) {
      vs[a][b] = vs[b][a] = 1;
      memset(gone, 0, sizeof(gone));
      if (dfs(1, n))
        nf++;
    } else {
      int fr = -1, to;
      if (vs[a][b] == 0) {
        fr = a;
        to = b;
      } else if (vs[b][a] == 0) {
        fr = b;
        to = a;
      }
      vs[a][b] = vs[b][a] = 0;
      a = 0;
      if (fr >= 0) {
        memset(gone, 0, sizeof(gone));
        if (!dfs(n, to))
          a = 1 / a;
        memset(gone, 0, sizeof(gone));
        if (!dfs(fr, 1))
          a = 1 / a;
        nf--;
        memset(gone, 0, sizeof(gone));
        if (dfs(1, n))
          nf++;
      }
    }
    printf("%d\\n", nf);
  }
  return 0;
}