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

#include <cstdio>

int N, M, x, y, a[12][12], sum[2][12], vis[12][12];
int qx[110], qy[110], DEP;

int dfs(int dep) {
  if (dep > DEP)
    return 1;

  int re = 0;

  sum[0][qx[dep]]++;
  sum[1][qy[dep]]++;
  if (sum[0][qx[dep]] <= N / 2 && sum[1][qy[dep]] <= N / 2)
    re += dfs(dep + 1);
  sum[0][qx[dep]]--;
  sum[1][qy[dep]]--;

  sum[1][qx[dep]]++;
  sum[0][qy[dep]]++;
  if (sum[1][qx[dep]] <= N / 2 && sum[0][qy[dep]] <= N / 2)
    re += dfs(dep + 1);
  sum[1][qx[dep]]--;
  sum[0][qy[dep]]--;

  return re;
}

int main() {
  while (1) {
    DEP = 0;
    scanf("%d", &N);
    if (N == 0)
      break;
    scanf("%d", &M);

    for (int i = 1; i <= N; ++i) {
      sum[0][i] = sum[1][i] = 0;
      for (int j = i; j <= N; ++j)
        vis[i][j] = vis[j][i] = 0;
    }

    for (int i = 1; i <= M; ++i) {
      scanf("%d%d", &x, &y);
      vis[x][y] = vis[y][x] = 1;
      a[x][y] = 1;
      a[y][x] = 0;
      sum[1][x]++;
      sum[0][y]++;
    }
    bool f = 0;
    for (int i = 1; i <= N; ++i) {
      if (sum[0][i] > N / 2 || sum[1][i] > N / 2) {
        printf("0\\n");
        f = 1;
        break;
      }
    }
    if (f)
      continue;

    for (int i = 1; i <= N; ++i) {
      for (int j = i + 1; j <= N; ++j) {
        if (!vis[i][j]) {
          qx[++DEP] = i;
          qy[DEP] = j;
        }
      }
    }

    printf("%d\\n", dfs(1));
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int d[10];
bool vis[10][10];
int n;
int mp1[10000000], mp2[10000000];
int a[50], b[50], x[10];
long long ans;
void dfs(int k, int up) {
  if (k == up + 1) {
    int res = 0;
    int j = 1;
    for (int i = n; i >= 1; i--) {
      res += d[i] * j;
      j *= 5;
    }
    mp1[res]++;
    return;
  }
  d[a[k]]++;
  if (d[a[k]] <= n / 2)
    dfs(k + 1, up);
  d[a[k]]--;
  d[b[k]]++;
  if (d[b[k]] <= n / 2)
    dfs(k + 1, up);
  d[b[k]]--;
}
void dfs2(int k, int up) {
  if (k == up + 1) {
    int res = 0;
    int j = 1;
    for (int i = n; i >= 1; i--) {
      res += d[i] * j;
      j *= 5;
    }
    mp2[res]++;
    return;
  }
  d[a[k]]++;
  if (d[a[k]] <= n / 2)
    dfs2(k + 1, up);
  d[a[k]]--;
  d[b[k]]++;
  if (d[b[k]] <= n / 2)
    dfs2(k + 1, up);
  d[b[k]]--;
}
void dfs3(int k) {
  if (k == n + 1) {
    int res = 0;
    int gg = 0;
    int j = 1;
    bool flag = true;
    for (int i = n; i >= 1; i--) {
      res += x[i] * j;
      gg += (n / 2 - x[i] + d[i]) * j;
      if (x[i] < d[i])
        flag = false;
      j *= 5;
    }
    if (flag)
      ans += (long long)mp1[res] * mp2[gg];
    return;
  }
  for (int i = 0; i <= n / 2; i++) {
    x[k] = i;
    dfs3(k + 1);
  }
}
int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    memset(d, 0, sizeof(d));
    memset(vis, false, sizeof(vis));
    memset(mp1, 0, sizeof(mp1));
    memset(mp2, 0, sizeof(mp2));
    ans = 0;
    int m;
    scanf("%d", &m);
    bool flag = true;
    for (int i = 1; i <= m; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      d[x]++;
      vis[x][y] = vis[y][x] = true;
      if (d[x] > n / 2) {
        flag = false;
        break;
      }
    }
    if (!flag) {
      printf("0\\n");
      continue;
    }
    int sz = 0;
    for (int i = 1; i <= n - 1; i++)
      for (int j = i + 1; j <= n; j++) {
        if (!vis[i][j]) {
          sz++;
          a[sz] = i;
          b[sz] = j;
        }
      }
    dfs(1, sz / 2);
    dfs2(sz / 2 + 1, sz);
    dfs3(1);
    printf("%lld\\n", ans);
  }
  return 0;
}