prithivMLmods/Coder-Stat huggingface dataset:
Problem p00741 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define pb(in, tmp) in.push_back(tmp)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) loop(i, 0, b)
#define all(in) in.begin(), in.end()
using namespace std;
void isl(int in[50][50], int x, int y) {
  in[x][y] = 0;
  loop(i, -1, 2) {
    loop(j, -1, 2) {
      if (x - i >= 0 && y - j >= 0 && in[x - i][y - j])
        isl(in, x - i, y - j);
    }
  }
}
int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    int in[50][50] = {0};
    int co = 0;
    rep(i, m) rep(j, n) cin >> in[i][j];

    rep(i, m) {
      rep(j, n) {
        if (in[i][j]) {
          co++;
          isl(in, i, j);
        }
      }
    }
    cout << co << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define pb(in, tmp) in.push_back(tmp)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) loop(i, 0, b)
#define all(in) in.begin(), in.end()
using namespace std;
void isl(int in[50][50], int x, int y) {
  in[x][y] = 0;
  loop(i, -1, 2) {
    loop(j, -1, 2) {
      if (x - i >= 0 && x - i < 50 && y - j >= 0 && y - j < 50 &&
          in[x - i][y - j])
        isl(in, x - i, y - j);
    }
  }
}
int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    int in[50][50] = {0};
    int co = 0;
    rep(i, m) rep(j, n) cin >> in[i][j];

    rep(i, m) {
      rep(j, n) {
        if (in[i][j]) {
          co++;
          isl(in, i, j);
        }
      }
    }
    cout << co << endl;
  }
}
Problem p00741 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

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

void dfs(int x, int y, vector<vector<bool>> &canGo) {
  canGo[x][y] = false;
  for (int i = 0; i < 9; i++)
    if (canGo[x + dx[i]][y + dy[i]])
      dfs(x + dx[i], y + dy[i], canGo);
  return;
}

int main(void) {
  while (true) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }

    int input;
    vector<vector<bool>> canGo(h + 2, vector<bool>(w + 2, false));
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> input;
        if (input == 1) {
          canGo[i][j] = true;
        }
      }
    }

    int count = 0;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (canGo[i][j]) {
          ++count;
          dfs(i, j, canGo);
        }
      }
    }

    cout << count << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

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

void dfs(int x, int y, vector<vector<bool>> &canGo) {
  canGo[x][y] = false;
  for (int i = 0; i < 8; i++)
    if (canGo[x + dx[i]][y + dy[i]])
      dfs(x + dx[i], y + dy[i], canGo);
  return;
}

int main(void) {
  while (true) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }

    int input;
    vector<vector<bool>> canGo(h + 2, vector<bool>(w + 2, false));
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> input;
        if (input == 1) {
          canGo[i][j] = true;
        }
      }
    }

    int count = 0;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (canGo[i][j]) {
          ++count;
          dfs(i, j, canGo);
        }
      }
    }

    cout << count << endl;
  }

  return 0;
}
Problem p00741 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)

using namespace std;

#ifdef _DEBUG
#define debug_io                                                               \
  fstream cin("input.txt");                                                    \
  ofstream cout("output.txt");
#else
#define debug_io ;
#endif

int main() {
  debug_io int w, h;
  while (cin >> w >> h, w + h) {
    int tile[55][55];
    REP(i, h) {
      REP(j, w) { cin >> tile[i][j]; }
    }

    int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
    int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

    int ans = 0;
    REP(i, h) {
      REP(j, w) {
        if (tile[i][j] == 1) {
          vector<pair<int, int>> bfs;
          bfs.push_back({i, j});

          while (bfs.size()) {
            auto now = bfs.front();
            bfs.erase(bfs.begin());
            int x = now.second;
            int y = now.first;
            tile[y][x] = 2;
            REP(k, 8) {
              int nx = max(0, min(x + dx[k], w - 1));
              int ny = max(0, min(y + dy[k], h - 1));
              if (tile[ny][nx] == 1) {
                bfs.push_back({ny, nx});
              }
            }
          }
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)

using namespace std;

#ifdef _DEBUG
#define debug_io                                                               \
  fstream cin("input.txt");                                                    \
  ofstream cout("output.txt");
#else
#define debug_io ;
#endif

int main() {
  debug_io int w, h;
  while (cin >> w >> h, w + h) {
    int tile[55][55];
    REP(i, h) {
      REP(j, w) { cin >> tile[i][j]; }
    }

    int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
    int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

    int ans = 0;
    REP(i, h) {
      REP(j, w) {
        if (tile[i][j] == 1) {
          vector<pair<int, int>> bfs;
          bfs.push_back({i, j});

          while (bfs.size()) {
            auto now = bfs.front();
            bfs.erase(bfs.begin());
            int x = now.second;
            int y = now.first;
            if (tile[y][x] == 2) {
              continue;
            }
            tile[y][x] = 2;
            REP(k, 8) {
              int nx = max(0, min(x + dx[k], w - 1));
              int ny = max(0, min(y + dy[k], h - 1));
              if (tile[ny][nx] == 1) {
                bfs.push_back({ny, nx});
              }
            }
          }
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00741 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdlib.h>

using namespace std;

int d[8][2] = {{-1, -1}, {0, -1}, {1, -1}, {-1, 0},
               {1, 0},   {-1, 1}, {0, 1},  {1, 1}};

void func(int **a, int s, int t, int i, int j) {
  if (a[i][j] == 0) {
    return;
  }
  a[i][j] = 0;

  for (int k = 0; k < 8; k++) {
    if ((i + d[k][0] >= 0 && i + d[k][0] < t) &&
        (j + d[k][1] >= 0 && j + d[k][1] < s)) {
      func(a, s, t, i + d[k][0], j + d[k][1]);
    }
  }

  return;
}

int main(int argc, char **argv) {
  int s, t, **a, c, t2, s2;

  while (1) {
    cin >> s >> t;
    if (s == 0 && t == 0) {
      break;
    } else if (s == 1 || t == 1) {
      t2 = t + 1;
      s2 = s + 1;
    } else {
      t2 = t;
      s2 = s;
    }
    a = (int **)calloc(t2, sizeof(int));
    for (int i = 0; i < t; i++) {
      a[i] = (int *)calloc(s2, sizeof(int));
      for (int j = 0; j < s; j++) {
        cin >> c;
        a[i][j] = c;
      }
    }
    c = 0;

    for (int i = 0; i < t; i++) {
      for (int j = 0; j < s; j++) {
        if (a[i][j] == 1) {
          c++;
          func(a, s, t, i, j);
        }
      }
    }
    cout << c << endl;

    for (int i = 0; i < s2; i++) {
      free(a[i]);
    }
    free(a);
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <stdlib.h>

using namespace std;

int d[8][2] = {{-1, -1}, {0, -1}, {1, -1}, {-1, 0},
               {1, 0},   {-1, 1}, {0, 1},  {1, 1}};

void func(int **a, int s, int t, int i, int j) {
  if (a[i][j] == 0) {
    return;
  }
  a[i][j] = 0;

  for (int k = 0; k < 8; k++) {
    if ((i + d[k][0] >= 0 && i + d[k][0] < t) &&
        (j + d[k][1] >= 0 && j + d[k][1] < s)) {
      func(a, s, t, i + d[k][0], j + d[k][1]);
    }
  }

  return;
}

int main(int argc, char **argv) {
  int s, t, **a, c, t2, s2;

  while (1) {
    cin >> s >> t;
    if (s == 0 && t == 0) {
      break;
    } else if (s == 1 || t == 1) {
      t2 = t + 1;
      s2 = s + 1;
    } else {
      t2 = t;
      s2 = s;
    }
    a = (int **)calloc(t2, sizeof(int));
    for (int i = 0; i < t; i++) {
      a[i] = (int *)calloc(s2, sizeof(int));
      for (int j = 0; j < s; j++) {
        cin >> c;
        a[i][j] = c;
      }
    }
    c = 0;

    for (int i = 0; i < t; i++) {
      for (int j = 0; j < s; j++) {
        if (a[i][j] == 1) {
          c++;
          func(a, s, t, i, j);
        }
      }
    }
    cout << c << endl;

    for (int i = 0; i < t2; i++) {
      free(a[i]);
    }
    free(a);
  }

  return 0;
}
Problem p00741 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#define f(n) for (int i = 0; i < n; i++)
#define s(w) scanf("%d", &w)
int a, n, m, d[99][99];
int u[16] = {-1, -1, -1, 0, 0, 1, 1, 1, -1, 0, 1, -1, 1, -1, 0, 1};
void k(int x, int y) {
  if (d[x][y]) {
    d[x][y] = 0;
    f(8) k(x + u[i], y + u[i + 8]);
  }
}
int main() {
  a = 0;
  s(n);
  s(m);
  f(9801) d[0][i] = 0;
  f(n * m) s(d[i / n + 1][i % n + 1]);
  f(n * m) if (d[i / n + 1][i % n + 1]) a++, k(i / n + 1, i % n + 1);
  printf("%d\n", a);
  main();
}

Fixed submission:
#include <cstdio>
#define f(n) for (int i = 0; i < n; i++)
#define s(w) scanf("%d", &w)
int a, n, m, d[99][99];
int u[16] = {-1, -1, -1, 0, 0, 1, 1, 1, -1, 0, 1, -1, 1, -1, 0, 1};
void k(int x, int y) {
  if (d[x][y]) {
    d[x][y] = 0;
    f(8) k(x + u[i], y + u[i + 8]);
  }
}
int main() {
  a = 0;
  s(n);
  s(m);
  if (n == 0)
    return 0;
  f(9801) d[0][i] = 0;
  f(n * m) s(d[i / n + 1][i % n + 1]);
  f(n * m) if (d[i / n + 1][i % n + 1]) a++, k(i / n + 1, i % n + 1);
  printf("%d\n", a);
  main();
}
Problem p00741 (C++)

Buggy submission (status: Runtime Error):
// 41
#include <iostream>

using namespace std;

int w, h;
bool imap[50][50];

void ff(int y, int x) {
  imap[y][x] = false;
  for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
      if (imap[y + i][x + j]) {
        ff(y + i, x + j);
      }
    }
  }
}

int main() {
  for (; cin >> w >> h, w | h;) {
    fill_n(&imap[0][0], 50 * 50, false);
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> imap[y][x];
      }
    }
    int ans = 0;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        if (imap[y][x]) {
          ans++;
          ff(y, x);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
// 41
#include <iostream>

using namespace std;

int w, h;
bool imap[50][50];

void ff(int y, int x) {
  imap[y][x] = false;
  for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
      int ny = y + i, nx = x + j;
      if (0 <= ny && ny < h && 0 <= nx && nx < w && imap[ny][nx]) {
        ff(ny, nx);
      }
    }
  }
}

int main() {
  for (; cin >> w >> h, w | h;) {
    fill_n(&imap[0][0], 50 * 50, false);
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> imap[y][x];
      }
    }
    int ans = 0;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        if (imap[y][x]) {
          ans++;
          ff(y, x);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00741 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
using namespace std;

int map[20][20];

typedef pair<int, int> pxy;

int h, w, ans = 0;

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

void bfs(int sx, int sy) {
  queue<pxy> q;
  q.push(pxy(sx, sy));
  while (!q.empty()) {
    int x, y;
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if (map[x][y]) {
      map[x][y] = 0;
      for (int i = 0; i < 8; i++) {
        int next_x = x + dx[i], next_y = y + dy[i];
        if (map[next_x][next_y] && next_x < w && next_x >= 0 && next_y < h &&
            next_y >= 0) {
          q.push(pxy(next_x, next_y));
        }
      }
    }
  }
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    } else {
      for (int i = 0; i < h; i++)
        for (int j = 0; j < w; j++)
          cin >> map[j][i];
      for (int i = 0; i < h; i++)
        for (int j = 0; j < w; j++)
          if (map[j][i]) {
            ans++;
            bfs(j, i);
          }
      cout << ans << endl;
      ans = 0;
    }
  }
}

Fixed submission:
#include <iostream>
#include <queue>
using namespace std;

int map[50][50];

typedef pair<int, int> pxy;

int h, w, ans = 0;

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

void bfs(int sx, int sy) {
  queue<pxy> q;
  q.push(pxy(sx, sy));
  while (!q.empty()) {
    int x, y;
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if (map[x][y]) {
      map[x][y] = 0;
      for (int i = 0; i < 8; i++) {
        int next_x = x + dx[i], next_y = y + dy[i];
        if (map[next_x][next_y] && next_x < w && next_x >= 0 && next_y < h &&
            next_y >= 0) {
          q.push(pxy(next_x, next_y));
        }
      }
    }
  }
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    } else {
      for (int i = 0; i < h; i++)
        for (int j = 0; j < w; j++)
          cin >> map[j][i];
      for (int i = 0; i < h; i++)
        for (int j = 0; j < w; j++)
          if (map[j][i]) {
            ans++;
            bfs(j, i);
          }
      cout << ans << endl;
      ans = 0;
    }
  }
}
Problem p00741 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

const double eps = 1e-10;

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

int main() {

  int w, h;
  while (cin >> w >> h, w) {
    vector<vector<int>> table(h, vector<int>(w));
    int res = 0;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++)
        cin >> table[y][x];
    }

    queue<pair<int, int>> q;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {

        if (table[i][j] == 0)
          continue;

        res++;
        q.push(make_pair(i, j));

        while (!q.empty()) {
          pair<int, int> p = q.front();
          q.pop();
          int y = p.first, x = p.second;

          table[y][x] = 0;
          for (int k = 0; k < 8; k++) {
            int nx = x + dx[k], ny = y + dy[k];
            if (0 <= nx && nx < w && 0 <= ny && ny < h && table[ny][nx] == 1)
              q.push(make_pair(ny, nx));
          }
        }
      }
    }
    cout << res << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

const double eps = 1e-10;

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

int main() {

  int w, h;
  while (cin >> w >> h, w) {
    vector<vector<int>> table(h, vector<int>(w));
    int res = 0;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++)
        cin >> table[y][x];
    }

    queue<pair<int, int>> q;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {

        if (table[i][j] == 0)
          continue;

        res++;
        q.push(make_pair(i, j));

        while (!q.empty()) {
          pair<int, int> p = q.front();
          q.pop();
          int y = p.first, x = p.second;

          //					cout<<y<<" "<<x<<endl;

          if (table[y][x] == 0)
            continue;

          table[y][x] = 0;
          for (int k = 0; k < 8; k++) {
            int nx = x + dx[k], ny = y + dy[k];
            if (0 <= nx && nx < w && 0 <= ny && ny < h && table[ny][nx] == 1)
              q.push(make_pair(ny, nx));
          }
        }
      }
    }
    cout << res << endl;
  }

  return 0;
}
Problem p00741 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

int a[50][50] = {};
int w, h;

void island(int x, int y) {
  a[x][y] = 0;
  for (int i = -1; i < 2; i++) {
    for (int j = -1; j < 2; y++) {
      if (y + j < 0 || y + j > h - 1 || x + i > w - 1 || x + i < 0) {
        continue;
      }
      if (a[x + i][y + j] == 1) {
        island(x + i, y + j);
      }
    }
  }
}

int main() {
  while (cin >> w >> h, !(w == 0 && h == 0)) {
    REP(i, h) {
      REP(j, w) { cin >> a[j][i]; }
    }
    int ans(0);
    REP(i, h) {
      REP(j, w) {
        if (a[j][i] == 1) {
          island(j, i);
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

int a[50][50] = {};
int w, h;

void island(int x, int y) {
  a[x][y] = 0;
  for (int i = -1; i < 2; i++) {
    for (int j = -1; j < 2; j++) {
      if (y + j < 0 || y + j > h - 1 || x + i > w - 1 || x + i < 0) {
        continue;
      }
      if (a[x + i][y + j] == 1) {
        island(x + i, y + j);
      }
    }
  }
}

int main() {
  while (cin >> w >> h, !(w == 0 && h == 0)) {
    REP(i, h) {
      REP(j, w) { cin >> a[j][i]; }
    }
    int ans(0);
    REP(i, h) {
      REP(j, w) {
        if (a[j][i] == 1) {
          island(j, i);
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00741 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, p, n) for (int i = (int)p; i < (int)n; i++)
#define repp(i, p, n) for (int i = (int)p; i >= (int)n; i--)
#define Sort(x) sort(x.begin(), x.end())
#define PSort(x) sort(begin(x), end(x), pairCompare)
#define ll long long
#define INF 2000000000
#define pb push_back
#define mp make_pair
#define PI 3.14159265358979
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
using namespace std;

vector<vector<int>> v;
int w, h;

void dfs(int y, int x) {
  if (v[y][x] == 1)
    v[y][x] = 0;

  for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
      int nx = x + i, ny = y + j;

      if (!(nx < 0 || ny < 0 || nx >= w || ny >= h) && v[ny][nx] == 1)
        dfs(ny, nx);
    }
  }

  return;
}

int main() {
  while (1) {
    int ans = 0;
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    v.resize(h, vector<int>(w));
    rep(i, 0, h) {
      rep(j, 0, w) { cin >> v[i][j]; }
    }

    rep(i, 0, h) {
      rep(j, 0, w) {
        if (v[i][j] == 1) {
          ans++;
          dfs(i, j);
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, p, n) for (int i = (int)p; i < (int)n; i++)
#define repp(i, p, n) for (int i = (int)p; i >= (int)n; i--)
#define Sort(x) sort(x.begin(), x.end())
#define PSort(x) sort(begin(x), end(x), pairCompare)
#define ll long long
#define INF 2000000000
#define pb push_back
#define mp make_pair
#define PI 3.14159265358979
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
using namespace std;

vector<vector<int>> v;
int w, h;

void dfs(int y, int x) {
  if (v[y][x] == 1)
    v[y][x] = 0;

  for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
      int nx = x + i, ny = y + j;

      if (!(nx < 0 || ny < 0 || nx >= w || ny >= h) && v[ny][nx] == 1)
        dfs(ny, nx);
    }
  }

  return;
}

int main() {
  while (1) {
    int ans = 0;
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    v.resize(h, vector<int>(w));
    rep(i, 0, h) {
      rep(j, 0, w) { cin >> v[i][j]; }
    }

    rep(i, 0, h) {
      rep(j, 0, w) {
        if (v[i][j] == 1) {
          ans++;
          dfs(i, j);
        }
      }
    }
    cout << ans << endl;
    v.clear();
  }

  return 0;
}
Problem p00742 (C++)

Buggy submission (status: Runtime Error):
//=================================
// Created on: 2018/07/04 15:12:12
//=================================
#include <bits/stdc++.h>
using namespace std;
int main() {
  for (int N;;) {
    cin >> N;
    if (N == 0) {
      break;
    }
    vector<int> coeff1(10, 0);
    vector<int> coeff2(10, 0);
    set<int> nz;
    map<char, int> ind;
    for (int i = 0; i < N; i++) {
      string s;
      cin >> s;
      reverse(s.begin(), s.end());
      for (int j = 0, d = 1; j < s.size(); j++, d *= 10) {
        const char c = s[j];
        if (ind.find(c) == ind.end()) {
          ind[c] = ind.size();
        }
        const int in = ind[c];
        (i < N - 1 ? coeff1 : coeff2).at(in) += d;
        if (j == s.size() - 1 and s.size() > 1) {
          nz.insert(in);
        }
      }
    }
    assert(ind.size() <= 10);
    vector<int> num{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int div = 1;
    for (int i = 0; i < 10 - (int)ind.size(); i++) {
      div *= (i + 1);
    }
    int ans = 0;
    do {
      bool ok = true;
      for (const int nzz : nz) {
        if (num[nzz] == 0) {
          ok = false;
          break;
        }
      }
      if (not ok) {
        continue;
      }
      int left = 0, right = 0;
      for (int i = 0; i < ind.size(); i++) {
        left += coeff1[i] * num[i], right += coeff2[i] * num[i];
      }
      if (left == right) {
        ans++;
      }
    } while (next_permutation(num.begin(), num.end()));
    cout << ans / div << endl;
  }
  return 0;
}


Fixed submission:
//=================================
// Created on: 2018/07/04 15:12:12
//=================================
#include <bits/stdc++.h>
using namespace std;
int main() {
  for (int N;;) {
    cin >> N;
    if (N == 0) {
      break;
    }
    vector<int> coeff1(10, 0);
    vector<int> coeff2(10, 0);
    set<int> nz;
    map<char, int> ind;
    for (int i = 0; i < N; i++) {
      string s;
      cin >> s;
      reverse(s.begin(), s.end());
      for (int j = 0, d = 1; j < s.size(); j++, d *= 10) {
        const char c = s[j];
        if (ind.find(c) == ind.end()) {
          const int s = ind.size();
          ind[c] = s;
        }
        const int in = ind[c];
        (i < N - 1 ? coeff1 : coeff2).at(in) += d;
        if (j == s.size() - 1 and s.size() > 1) {
          nz.insert(in);
        }
      }
    }
    assert(ind.size() <= 10);
    vector<int> num{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int div = 1;
    for (int i = 0; i < 10 - (int)ind.size(); i++) {
      div *= (i + 1);
    }
    int ans = 0;
    do {
      bool ok = true;
      for (const int nzz : nz) {
        if (num[nzz] == 0) {
          ok = false;
          break;
        }
      }
      if (not ok) {
        continue;
      }
      int left = 0, right = 0;
      for (int i = 0; i < ind.size(); i++) {
        left += coeff1[i] * num[i], right += coeff2[i] * num[i];
      }
      if (left == right) {
        ans++;
      }
    } while (next_permutation(num.begin(), num.end()));
    cout << ans / div << endl;
  }
  return 0;
}
Problem p00742 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(s) s.begin(), s.end()

int main(void) {
  int n;
  while (cin >> n, n) {
    vector<string> s(n);
    map<char, int> a;
    map<char, bool> not_zero;
    rep(i, n) {
      cin >> s[i];
      int k = s[i].size();
      if (k > 1)
        not_zero[s[i][0]] = true;
      reverse(all(s[i]));
      rep(j, k) { a[s[i][j]] += pow(10, j) * (i == n - 1 ? -1 : 1); }
    }

    set<char> st;
    rep(i, n) {
      for (char c : s[i]) {
        st.insert(c);
      }
    }
    string char_list;
    for (char c : st) {
      char_list += c;
      // cerr<<a[c]<<" ";
    }
    // cerr<<endl;

    char_list += string(10 - (int)st.size(), '-');
    sort(all(char_list));
    int res = 0;

    int perm = 0;
    do {
      perm++;
      if (not_zero[char_list[0]])
        continue;
      int sum = 0;
      rep(i, 10) sum += i * a[char_list[i]];
      if (sum == 0)
        res++;
    } while (next_permutation(all(char_list)));
    cout << res << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(s) s.begin(), s.end()

int main(void) {
  int n;
  while (cin >> n, n) {
    vector<string> s(n);
    // map<char,int> a;
    int a[256] = {};
    // map<char,bool> not_zero;
    bool not_zero[256] = {};
    rep(i, n) {
      cin >> s[i];
      int k = s[i].size();
      if (k > 1)
        not_zero[s[i][0]] = true;
      reverse(all(s[i]));
      rep(j, k) { a[s[i][j]] += pow(10, j) * (i == n - 1 ? -1 : 1); }
    }

    set<char> st;
    rep(i, n) {
      for (char c : s[i]) {
        st.insert(c);
      }
    }
    string char_list;
    for (char c : st) {
      char_list += c;
      // cerr<<a[c]<<" ";
    }
    // cerr<<endl;

    char_list += string(10 - (int)st.size(), '-');
    sort(all(char_list));
    int res = 0;

    int perm = 0;
    do {
      perm++;
      if (not_zero[char_list[0]])
        continue;
      int sum = 0;
      rep(i, 10) sum += i * a[char_list[i]];
      if (sum == 0)
        res++;
    } while (next_permutation(all(char_list)));
    cout << res << endl;
  }

  return 0;
}
Problem p00742 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int m, coef[10];
bool zero[10];

int dfs(int idx, int sum, int used) {
  if (idx == m)
    return sum == 0;

  int cnt = 0;
  rep(i, 10) {
    if (used & (1 << i) || (i == 0 && !zero[idx]))
      continue;
    cnt += dfs(idx + 1, sum + coef[idx] * i, used | (1 << i));
  }
  return cnt;
}

int main() {
  for (int n; cin >> n, n;) {
    int f[128], idx = 0;
    fill(f, f + 128, -1);
    fill(coef, coef + 10, 0);
    fill(zero, zero + 10, true);
    rep(i, n) {
      string num;
      cin >> num;
      int sign = (i < n - 1 ? 1 : -1);
      for (int j = num.length() - 1, d = 1; j >= 0; j--, d *= 10) {
        if (f[num[j]] == -1)
          f[num[j]] = idx++;
        coef[f[num[j]]] += sign * d;
      }
      if (num.length() > 1)
        zero[f[num[0]]] = false;
    }
    m = idx;
    cout << dfs(0, 0, 0) << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int m, coef[10];
bool zero[10];

int dfs(int idx, int sum, int used) {
  if (idx == m)
    return sum == 0;

  int pr_max = 0, pr_min = 0;
  for (int i = idx; i < m; i++) {
    if (coef[i] > 0)
      pr_max += coef[i] * 9;
    if (coef[i] < 0)
      pr_min += coef[i] * 9;
  }
  if (sum > 0 && sum + pr_min > 0)
    return 0;
  if (sum < 0 && sum + pr_max < 0)
    return 0;

  int cnt = 0;
  rep(i, 10) {
    if (used & (1 << i) || (i == 0 && !zero[idx]))
      continue;
    cnt += dfs(idx + 1, sum + coef[idx] * i, used | (1 << i));
  }
  return cnt;
}

int main() {
  for (int n; cin >> n, n;) {
    int f[128], idx = 0;
    fill(f, f + 128, -1);
    fill(coef, coef + 10, 0);
    fill(zero, zero + 10, true);
    rep(i, n) {
      string num;
      cin >> num;
      int sign = (i < n - 1 ? 1 : -1);
      for (int j = num.length() - 1, d = 1; j >= 0; j--, d *= 10) {
        if (f[num[j]] == -1)
          f[num[j]] = idx++;
        coef[f[num[j]]] += sign * d;
      }
      if (num.length() > 1)
        zero[f[num[0]]] = false;
    }
    m = idx;
    cout << dfs(0, 0, 0) << endl;
  }
  return 0;
}
Problem p00742 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int n, m, a[10], f[10], u[10];

int rec(int k, int t) {
  if (k == m)
    return t == 0 ? 1 : 0;
  int ans = 0;
  rep(i, 10) if (u[i] == 0) {
    if (i == 0 && f[k])
      continue;
    u[i] = 1;
    ans += rec(k + 1, t + a[k] * i);
    u[i] = 0;
  }
  return ans;
}

int main() {
  for (;;) {
    cin >> n;
    if (n == 0)
      return 0;
    memset(a, 0, sizeof(a));
    memset(f, 0, sizeof(f));
    map<char, int> of;
    m = 0;
    rep(k, n) {
      string s;
      cin >> s;
      int t = 1;
      for (int i = s.size() - 1; i >= 0; i--) {
        if (of.count(s[i]) == 0)
          of[s[i]] = m++;
        a[of[s[i]]] += k < n - 1 ? t : -t;
        if (s.size() > 1 && i == 0)
          f[of[s[i]]] = 1;
        t *= 10;
      }
    }
    printf("%d\n", rec(0, 0));
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int n, m, a[10], f[10], u[10];

int rec(int k, int t) {
  if (k == m)
    return t == 0 ? 1 : 0;
  int mx = 0, mn = 0;
  for (int i = k; i < m; i++) {
    if (a[i] > 0)
      mx += a[i] * 9;
    else
      mn += a[i] * 9;
  }
  if (t + mx < 0 || t + mn > 0)
    return 0;
  int ans = 0;
  rep(i, 10) if (u[i] == 0) {
    if (i == 0 && f[k])
      continue;
    u[i] = 1;
    ans += rec(k + 1, t + a[k] * i);
    u[i] = 0;
  }
  return ans;
}

int main() {
  for (;;) {
    cin >> n;
    if (n == 0)
      return 0;
    memset(a, 0, sizeof(a));
    memset(f, 0, sizeof(f));
    map<char, int> of;
    m = 0;
    rep(k, n) {
      string s;
      cin >> s;
      int t = 1;
      for (int i = s.size() - 1; i >= 0; i--) {
        if (of.count(s[i]) == 0)
          of[s[i]] = m++;
        a[of[s[i]]] += k < n - 1 ? t : -t;
        if (s.size() > 1 && i == 0)
          f[of[s[i]]] = 1;
        t *= 10;
      }
    }
    printf("%d\n", rec(0, 0));
  }
}
Problem p00744 (C++)

Buggy submission (status: Runtime Error):
#include <list>
#include <stdio.h>
#include <string.h>
int F(int x, int y) {
  if (!y)
    return x;
  return F(y, x % y);
}
typedef std::list<int> L;
L e[1002];
int m, n;
bool f[1002];
int G(int s, int d) {
  if (s == d)
    return 1;
  if (f[s])
    return 0;
  f[s] = 1;
  for (L::iterator i = e[s].begin(); i != e[s].end(); ++i) {
    if (G(*i, d)) {
      e[*i].push_back(s);
      e[s].erase(i);
      return 1;
    }
  }
  return 0;
}
int main() {
  int a[100], i, j, x;
  while (scanf("%d%d", &m, &n), m) {
    for (i = 0; i < m + n + 2; ++i)
      e[i].clear();
    for (i = 0; i < m; ++i)
      scanf("%d", &a[i]), e[m + n].push_back(i);
    for (j = 0; j < n; ++j) {
      scanf("%d", &x);
      for (i = 0; i < m; ++i)
        if (F(a[i], x) > 1)
          e[i].push_back(m + j);
      e[m + j].push_back(m + n + 1);
    }
    for (x = 0; memset(f, 0, sizeof(f)), G(m + n, m + n + 1);)
      ++x;
    printf("%d\n", x);
  }
  return 0;
}

Fixed submission:
#include <list>
#include <stdio.h>
#include <string.h>
int F(int x, int y) {
  if (!y)
    return x;
  return F(y, x % y);
}
typedef std::list<int> L;
L e[1002];
int m, n;
bool f[1002];
int G(int s, int d) {
  if (s == d)
    return 1;
  if (f[s])
    return 0;
  f[s] = 1;
  for (L::iterator i = e[s].begin(); i != e[s].end(); ++i) {
    if (G(*i, d)) {
      e[*i].push_back(s);
      e[s].erase(i);
      return 1;
    }
  }
  return 0;
}
int main() {
  int a[500], i, j, x;
  while (scanf("%d%d", &m, &n), m) {
    for (i = 0; i < m + n + 2; ++i)
      e[i].clear();
    for (i = 0; i < m; ++i)
      scanf("%d", &a[i]), e[m + n].push_back(i);
    for (j = 0; j < n; ++j) {
      scanf("%d", &x);
      for (i = 0; i < m; ++i)
        if (F(a[i], x) > 1)
          e[i].push_back(m + j);
      e[m + j].push_back(m + n + 1);
    }
    for (x = 0; memset(f, 0, sizeof(f)), G(m + n, m + n + 1);)
      ++x;
    printf("%d\n", x);
  }
  return 0;
}
Problem p00744 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int gcd(int a, int b) {
  if (!b)
    return a;
  return gcd(b, a % b);
}
int b[500], r[500];
vector<int> E[500];
bool used[1000];
int match[1000];

bool dfs(int u) {
  used[u] = true;
  for (int v : E[u]) {
    int w = match[v];
    if (w == -1 || (!used[w] && dfs(w))) {
      match[u] = v;
      match[v] = u;
      return true;
    }
  }
  return false;
}
int main() {
  int m, n;
  while (scanf("%d%d", &m, &n), m) {
    rep(i, 1000) E[i].clear();
    rep(i, m) scanf("%d", &b[i]);
    rep(i, n) scanf("%d", &r[i]);
    rep(i, m) rep(j, n) {
      if (gcd(b[i], r[j]) != 1)
        E[i].push_back(m + j);
    }
    memset(match, -1, sizeof(match));
    int ans = 0;
    rep(i, m) {
      if (match[i] == -1) {
        memset(used, 0, sizeof(used));
        if (dfs(i))
          ans++;
      }
    }
    printf("%d\n", ans);
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int gcd(int a, int b) {
  if (!b)
    return a;
  return gcd(b, a % b);
}
int b[500], r[500];
vector<int> E[1000];
bool used[1000];
int match[1000];

bool dfs(int u) {
  used[u] = true;
  for (int v : E[u]) {
    int w = match[v];
    if (w == -1 || (!used[w] && dfs(w))) {
      match[u] = v;
      match[v] = u;
      return true;
    }
  }
  return false;
}
int main() {
  int m, n;
  while (scanf("%d%d", &m, &n), m) {
    rep(i, 1000) E[i].clear();
    rep(i, m) scanf("%d", &b[i]);
    rep(i, n) scanf("%d", &r[i]);
    rep(i, m) rep(j, n) {
      if (gcd(b[i], r[j]) != 1)
        E[i].push_back(m + j);
    }
    memset(match, -1, sizeof(match));
    int ans = 0;
    rep(i, m) {
      if (match[i] == -1) {
        memset(used, 0, sizeof(used));
        if (dfs(i))
          ans++;
      }
    }
    printf("%d\n", ans);
  }
}
Problem p00746 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

bool SortXPos(pair<int, int> pos1, pair<int, int> pos2) {
  return pos1.first < pos2.first ? true : false;
}
bool SortYPos(pair<int, int> pos1, pair<int, int> pos2) {
  return pos1.second < pos2.second ? true : false;
}

int main(void) {
  int num;
  while (cin >> num) {
    vector<pair<int, int>> pos(num);
    pos[0].first = 0;
    pos[0].second = 0;

    for (int i = 1; i < num; i++) {
      int base, action;
      cin >> base >> action;

      switch (action) {
      case 0:
        pos[i].first = pos[base].first - 1;
        pos[i].second = pos[base].second;
        break;
      case 1:
        pos[i].first = pos[base].first;
        pos[i].second = pos[base].second - 1;
        break;
      case 2:
        pos[i].first = pos[base].first + 1;
        pos[i].second = pos[base].second;
        break;
      case 3:
        pos[i].first = pos[base].first;
        pos[i].second = pos[base].second + 1;
        break;
      }
    }

    sort(pos.begin(), pos.end(), SortXPos);
    int xlength = pos[num - 1].first - pos[0].first + 1;
    sort(pos.begin(), pos.end(), SortYPos);
    int ylength = pos[num - 1].second - pos[0].second + 1;

    cout << xlength << ' ' << ylength << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

bool SortXPos(pair<int, int> pos1, pair<int, int> pos2) {
  return pos1.first < pos2.first ? true : false;
}
bool SortYPos(pair<int, int> pos1, pair<int, int> pos2) {
  return pos1.second < pos2.second ? true : false;
}

int main(void) {
  int num;
  while (cin >> num) {
    if (num == 0)
      break;
    vector<pair<int, int>> pos(num);
    pos[0].first = 0;
    pos[0].second = 0;

    for (int i = 1; i < num; i++) {
      int base, action;
      cin >> base >> action;

      switch (action) {
      case 0:
        pos[i].first = pos[base].first - 1;
        pos[i].second = pos[base].second;
        break;
      case 1:
        pos[i].first = pos[base].first;
        pos[i].second = pos[base].second - 1;
        break;
      case 2:
        pos[i].first = pos[base].first + 1;
        pos[i].second = pos[base].second;
        break;
      case 3:
        pos[i].first = pos[base].first;
        pos[i].second = pos[base].second + 1;
        break;
      }
    }

    sort(pos.begin(), pos.end(), SortXPos);
    int xlength = pos[num - 1].first - pos[0].first + 1;
    sort(pos.begin(), pos.end(), SortYPos);
    int ylength = pos[num - 1].second - pos[0].second + 1;

    cout << xlength << ' ' << ylength << endl;
  }
  return 0;
}
Problem p00746 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

struct POINT {
  int x;
  int y;
};

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

int main(void) {
  int N;
  while (cin >> N, N) {
    POINT minp = {0, 0};
    POINT maxp = {0, 0};

    vector<POINT> box(N);
    box[0].x = 0;
    box[0].y = 0;
    for (int i = 1; i < N; i++) {
      int n, d;
      cin >> n >> d;
      box[i].x = box[n].x + dx[d];
      box[i].y = box[n].y + dy[d];

      minp.x = min(minp.x, box[i].x);
      minp.y = min(minp.y, box[i].y);
      maxp.x = max(maxp.x, box[i].x);
      maxp.y = max(maxp.y, box[i].y);
    }

    cerr << "--- ";
    cout << (maxp.x - minp.x + 1) << " " << (maxp.y - minp.y + 1) << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

struct POINT {
  int x;
  int y;
};

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

int main(void) {
  int N;
  while (cin >> N, N) {
    POINT minp = {0, 0};
    POINT maxp = {0, 0};

    vector<POINT> box(N);
    box[0].x = 0;
    box[0].y = 0;
    for (int i = 1; i < N; i++) {
      int n, d;
      cin >> n >> d;
      box[i].x = box[n].x + dx[d];
      box[i].y = box[n].y + dy[d];

      minp.x = min(minp.x, box[i].x);
      minp.y = min(minp.y, box[i].y);
      maxp.x = max(maxp.x, box[i].x);
      maxp.y = max(maxp.y, box[i].y);
    }

    // cerr << "--- ";
    cout << (maxp.x - minp.x + 1) << " " << (maxp.y - minp.y + 1) << endl;
  }

  return 0;
}
Problem p00746 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using LL = long long;
using P = pair<int, int>;
using Tapris = tuple<int, int, int>;

#define REP(i, n) for (LL i = 0; i < n; ++i)
#define FOR(i, a, n) for (LL i = a; i < n; ++i)
#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()

const int INF = (int)1e9;
const LL INFL = (LL)1e15;
const int MOD = 1e9 + 7;

int dy[] = {0, -1, 0, 1};
int dx[] = {-1, 0, 1, 0};

// #define int long long

/*************** using variables ***************/
int N;
vector<int> n, d;
P coordinates[101];
/**********************************************/

void solve() {
  coordinates[0] = P(0, 0);
  REP(i, N - 1) {
    int cur = i + 1;
    int y = coordinates[n[i]].first + dy[d[i]];
    int x = coordinates[n[i]].second + dx[d[i]];
    coordinates[cur] = P(y, x);
  }

  int x_max_num = -INF;
  int y_max_num = -INF;
  int x_min_num = INF;
  int y_min_num = INF;
  REP(i, N) {
    x_max_num = max(x_max_num, coordinates[i].second);
    x_min_num = min(x_min_num, coordinates[i].second);
    y_max_num = max(y_max_num, coordinates[i].first);
    y_min_num = min(y_min_num, coordinates[i].first);
  }

  cout << x_max_num - x_min_num + 1 << " " << y_max_num - y_min_num + 1 << endl;
  return;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> N, N) {
    n.resize(N - 1);
    d.resize(N - 1);
    REP(i, N - 1) cin >> n[i] >> d[i];
    solve();
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using LL = long long;
using P = pair<int, int>;
using Tapris = tuple<int, int, int>;

#define REP(i, n) for (LL i = 0; i < n; ++i)
#define FOR(i, a, n) for (LL i = a; i < n; ++i)
#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()

const int INF = (int)1e9;
const LL INFL = (LL)1e15;
const int MOD = 1e9 + 7;

int dy[] = {0, -1, 0, 1};
int dx[] = {-1, 0, 1, 0};

// #define int long long

/*************** using variables ***************/
int N;
vector<int> n, d;
P coordinates[201];
/**********************************************/

void solve() {
  coordinates[0] = P(0, 0);
  REP(i, N - 1) {
    int cur = i + 1;
    int y = coordinates[n[i]].first + dy[d[i]];
    int x = coordinates[n[i]].second + dx[d[i]];
    coordinates[cur] = P(y, x);
  }

  int x_max_num = -INF;
  int y_max_num = -INF;
  int x_min_num = INF;
  int y_min_num = INF;
  REP(i, N) {
    x_max_num = max(x_max_num, coordinates[i].second);
    x_min_num = min(x_min_num, coordinates[i].second);
    y_max_num = max(y_max_num, coordinates[i].first);
    y_min_num = min(y_min_num, coordinates[i].first);
  }

  cout << x_max_num - x_min_num + 1 << " " << y_max_num - y_min_num + 1 << endl;
  return;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> N, N) {
    n.resize(N - 1);
    d.resize(N - 1);
    REP(i, N - 1) cin >> n[i] >> d[i];
    solve();
  }
}
Problem p00746 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#pragma warning(disable : 4996)
using namespace std;

const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
int main() {
  while (1) {
    int N;
    cin >> N;
    vector<int> xs;
    vector<int> ys;
    xs.emplace_back(0);
    ys.emplace_back(0);
    for (int i = 0; i < N - 1; ++i) {
      int n, d;
      cin >> n >> d;
      const int nx = xs[n] + dx[d];
      const int ny = ys[n] + dy[d];
      xs.emplace_back(nx);
      ys.emplace_back(ny);
    }
    const int width = *(max_element(xs.begin(), xs.end())) -
                      *(min_element(xs.begin(), xs.end()));
    const int height = *(max_element(ys.begin(), ys.end())) -
                       *(min_element(ys.begin(), ys.end()));
    cout << width + 1 << " " << height + 1 << endl;
  }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#pragma warning(disable : 4996)
using namespace std;

const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
int main() {
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    vector<int> xs;
    vector<int> ys;
    xs.emplace_back(0);
    ys.emplace_back(0);
    for (int i = 0; i < N - 1; ++i) {
      int n, d;
      cin >> n >> d;
      const int nx = xs[n] + dx[d];
      const int ny = ys[n] + dy[d];
      xs.emplace_back(nx);
      ys.emplace_back(ny);
    }
    const int width = *(max_element(xs.begin(), xs.end())) -
                      *(min_element(xs.begin(), xs.end()));
    const int height = *(max_element(ys.begin(), ys.end())) -
                       *(min_element(ys.begin(), ys.end()));
    cout << width + 1 << " " << height + 1 << endl;
  }
  return 0;
}
Problem p00748 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int dpt1[1000001];
int dpt2[1000001];
bool dpb1[1000001];
bool dpb2[1000001];
int tr[1000];
int sq[1000];

int dp1(int s) {
  if (dpb1[s])
    return dpt1[s];

  int m = 1000111000;
  int r;
  for (int i = 1; i < 1000; i++) {
    if (s - sq[i] < 0)
      break;
    r = dp1(s - sq[i]);
    if (r < m)
      m = r;
  }

  dpt1[s] = m + 1;
  dpb1[s] = true;
  return m + 1;
}

int dp2(int s) {
  if (dpb2[s])
    return dpt2[s];

  int m = 1000111000;
  int r;
  for (int i = 1; i < 1000; i++) {
    if (s - sq[i] < 0)
      break;
    if (sq[i] % 2 == 0)
      continue;
    r = dp2(s - sq[i]);
    if (r < m)
      m = r;
  }

  dpt2[s] = m + 1;
  dpb2[s] = true;
  return m + 1;
}

int main() {

  for (int i = 1; i < 1000; i++) {
    tr[i] = tr[i - 1] + i;
    sq[i] = sq[i - 1] + tr[i];
  }
  dpt1[0] = 0;
  dpb1[0] = true;
  dpt2[0] = 0;
  dpb2[0] = true;
  int n;
  while (cin >> n, n) {
    cout << dp1(n) << " " << dp2(n) << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int dpt1[1000001];
int dpt2[1000001];
bool dpb1[1000001];
bool dpb2[1000001];
int tr[1000];
int sq[1000];

int dp1(int s) {
  if (dpb1[s])
    return dpt1[s];

  int m = 1000111000;
  int r;
  for (int i = 1; i < 1000; i++) {
    if (s - sq[i] < 0)
      break;
    r = dp1(s - sq[i]);
    if (r < m)
      m = r;
  }

  dpt1[s] = m + 1;
  dpb1[s] = true;
  return m + 1;
}

int dp2(int s) {
  if (dpb2[s])
    return dpt2[s];

  int m = 1000111000;
  int r;
  for (int i = 1; i < 1000; i++) {
    if (s - sq[i] < 0)
      break;
    if (sq[i] % 2 == 0)
      continue;
    r = dp2(s - sq[i]);
    if (r < m)
      m = r;
  }

  dpt2[s] = m + 1;
  dpb2[s] = true;
  return m + 1;
}

int main() {

  for (int i = 1; i < 1000; i++) {
    tr[i] = tr[i - 1] + i;
    sq[i] = sq[i - 1] + tr[i];
  }
  dpt1[0] = 0;
  dpb1[0] = true;
  dpt2[0] = 0;
  dpb2[0] = true;
  int n;
  for (int i = 1; i <= 1000000; i++)
    dp1(i), dp2(i);
  while (cin >> n, n) {
    cout << dp1(n) << " " << dp2(n) << endl;
  }

  return 0;
}
Problem p00748 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#define INF 1 << 28
using namespace std;

int p[222];
int memo[1000001];
int memo2[1000001];

int solve(int n) {

  if (!n)
    return 0;
  if (memo[n])
    return memo[n];

  int ans = INF;
  for (int i = 1; p[i] <= n; i++)
    ans = min(ans, solve(n - p[i]) + 1);

  return memo[n] = ans;
}

int solve2(int n) {

  if (!n)
    return 0;
  if (memo2[n])
    return memo2[n];

  int ans = INF;
  for (int i = 1; p[i] <= n; i++) {
    if (p[i] & 1)
      ans = min(ans, solve2(n - p[i]) + 1);
  }

  return memo2[n] = ans;
}

int main() {

  int n;

  for (int i = 0; i <= 181; i++)
    p[i] = i * (i + 1) * (i + 2) / 6;
  // for(int i=1;i<=1000000;i++) solve(i), solve2(i);

  while (cin >> n, n)
    cout << solve(n) << " " << solve2(n) << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#define INF 1 << 28
using namespace std;

int p[222];
int memo[1000001];
int memo2[1000001];

int solve(int n) {

  if (!n)
    return 0;
  if (memo[n])
    return memo[n];

  int ans = INF;
  for (int i = 1; p[i] <= n; i++)
    ans = min(ans, solve(n - p[i]) + 1);

  return memo[n] = ans;
}

int solve2(int n) {

  if (!n)
    return 0;
  if (memo2[n])
    return memo2[n];

  int ans = INF;
  for (int i = 1; p[i] <= n; i++) {
    if (p[i] & 1)
      ans = min(ans, solve2(n - p[i]) + 1);
  }

  return memo2[n] = ans;
}

int main() {

  int n;

  for (int i = 0; i <= 181; i++)
    p[i] = i * (i + 1) * (i + 2) / 6;
  for (int i = 1; i <= 1000000; i += 100)
    solve(i), solve2(i);

  while (cin >> n, n)
    cout << solve(n) << " " << solve2(n) << endl;
}