alexjercan/bugnet huggingface dataset:
Problem p00156 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

struct STATE {
  int x, y, d;
  bool operator<(const STATE &t) const { return d > t.d; }
};

char grid[128][128];
int d[128][128];

bool ingrid(int x, int y, int w, int h) {
  return x >= 0 && x <= h + 1 && y >= 0 && y <= w + 1;
}

int main() {
  int w, h;
  while (scanf("%d %d", &w, &h), w + h) {
    memset(grid, '.', sizeof(grid));
    for (int i = 0; i < 128; i++) {
      for (int j = 0; j < 128; j++) {
        d[i][j] = INF;
      }
    }

    int sx, sy;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        scanf(" %c", &grid[i][j]);
        if (grid[i][j] == '&') {
          sx = i;
          sy = j;
        }
      }
    }

    int res = 0;
    priority_queue<STATE> que;
    for (que.push({sx, sy, 0}); !que.empty(); que.pop()) {
      STATE st = que.top();
      if (d[st.x][st.y] <= st.d)
        continue;
      d[st.x][st.y] = st.d;
      if (st.x == 0 || st.y == 0 || st.x == h + 1 || st.y == w + 1) {
        res = st.d;
        break;
      }
      for (int dir = 0; dir < 4; dir++) {
        int tx = st.x + dx[dir];
        int ty = st.y + dy[dir];
        if (!ingrid(tx, ty, w, h))
          continue;
        if (d[tx][ty] > st.d + (grid[st.x][st.y] == '#')) {
          que.push({tx, ty,
                    st.d + (grid[st.x][st.y] == '#' && grid[tx][ty] == '.')});
        }
      }
    }
    printf("%d\\n", res);
  }

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
struct STATE {
  int x, y, d;
  bool operator<(const STATE &t) const { return d > t.d; }
};

char grid[128][128];
int d[128][128];

int main() {
  int w, h;
  while (scanf("%d %d", &w, &h), w + h) {
    memset(grid, '.', sizeof(grid));
    for (int i = 0; i < 128; i++) {
      for (int j = 0; j < 128; j++) {
        d[i][j] = INF;
      }
    }

    int sx, sy;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        scanf(" %c", &grid[i][j]);
        if (grid[i][j] == '&') {
          sx = i;
          sy = j;
        }
      }
    }

    int res = 0;
    queue<STATE> que;
    for (que.push({sx, sy, 0}); !que.empty(); que.pop()) {
      STATE st = que.front();
      if (d[st.x][st.y] <= st.d)
        continue;
      d[st.x][st.y] = st.d;
      if (st.x == 0 || st.y == 0 || st.x == h + 1 || st.y == w + 1) {
        // res = st.d;
        // break;
      }
      for (int dir = 0; dir < 4; dir++) {
        int tx = st.x + dx[dir];
        int ty = st.y + dy[dir];
        if (d[tx][ty] > st.d + (grid[st.d][st.d] == '#')) {
          que.push({tx, ty, st.d + (grid[st.x][st.y] == '#')});
        }
      }
    }
    printf("%d\\n", d[0][0]);
  }

  return 0;
}
Problem p00156 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0}, w, h, t, d[101][101];
string s[101];
typedef pair<int, int> P;
queue<P> q;
void dfs(int a, int b, char p) {
  d[a][b] = t;
  r(i, 4) {
    int y = a + dy[i], x = b + dx[i];
    if (y >= 0 && x >= 0 && y < h && x < w) {
      if (d[y][x] == 1e9) {
        if (s[a][b] == s[y][x])
          dfs(y, x, p);
        else
          q.push(P(y, x));
      }
    }
  }
}
int main() {
  while (cin >> w >> h, w) {
    while (!q.empty())
      q.pop();
    r(i, h) r(j, w) d[i][j] = 1e9;
    r(i, h) cin >> s[i];
    r(i, h) r(j, w) if (s[i][j] == '&') {
      s[i][j] = '.';
      t = 0;
      dfs(i, j, s[i][j]);
    }
    r(i, 1e9) {
      if (i % 2 == 0)
        t++;
      if (q.empty())
        break;
      queue<P> pp;
      while (!q.empty())
        pp.push(q.front()), q.pop();
      while (!pp.empty()) {
        P pt = pp.front();
        pp.pop();
        int y = pt.first, x = pt.second;
        if (d[y][x] == 1e9)
          dfs(y, x, s[y][x]);
      }
    }
    int ans = 1e9;
    r(i, h) ans = min(ans, min(d[i][0], d[i][w - 1]));
    r(i, w) ans = min(ans, min(d[0][i], d[h - 1][i]));
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0}, w, h, t, d[101][101];
string s[101];
void dfs(int a, int b, char p) {
  d[a][b] = t;
  r(i, 4) {
    int y = a + dy[i], x = b + dx[i];
    if (y >= 0 && x >= 0 && y < h && x < w)
      if (d[y][x] == 1e9 && s[y][x] == p)
        dfs(y, x, p);
  }
}
int main() {
  while (cin >> w >> h, w) {
    r(i, h) r(j, w) d[i][j] = 1e9;
    r(i, h) cin >> s[i];
    r(i, h) r(j, w) if (s[i][j] == '&') {
      s[i][j] = '.';
      t = 0;
      dfs(i, j, s[i][j]);
    }
    r(o, h * w) r(i, h) r(j, w) if (d[i][j] != 1e9) r(k, 4) {
      int y = i + dy[k], x = j + dx[k];
      if (y < 0 || x < 0 || y >= h || x >= w)
        continue;
      if (s[i][j] == s[y][x])
        continue;
      if (s[i][j] == '.')
        t = d[i][j] + 1;
      else
        t = d[i][j];
      if (d[y][x] > t)
        dfs(y, x, s[i][j]);
    }
    int ans = 1e9;
    r(i, h) ans = min(ans, min(d[i][0], d[i][w - 1]));
    r(i, w) ans = min(ans, min(d[0][i], d[h - 1][i]));
    cout << ans << endl;
  }
}
Problem p00156 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
  int w, h;
  string line;
  while (true) {
    cin >> w >> h;
    if (!w && !h)
      break;
    vector<string> castle;
    for (int i = 0; i < h; i++) {
      cin >> line;
      castle.push_back(line);
    }
    int count[w + 2][h + 2];
    int sx, sy;
    const int MAX = 1000;
    for (int y = 1; y < h + 1; y++) {
      for (int x = 1; x < w + 1; x++) {
        count[x][y] = MAX;
        if (castle[y - 1][x - 1] == '&') {
          sx = x;
          sy = y;
          castle[y - 1][x - 1] = '.';
        }
      }
    }
    int min = MAX;
    int dx[4] = {1, 0, -1, 0};
    int dy[4] = {0, 1, 0, -1};
    count[sx][sy] = 0;
    stack<int> st;
    st.push(sy * (w + 2) + sx);
    while (!st.empty()) {
      int x = st.top() % (w + 2);
      int y = st.top() / (w + 2);
      st.pop();
      char c = castle[y - 1][x - 1];
      int n;
      for (int d = 0; d < 4; d++) {
        n = count[x][y];
        if (x + dx[d] > 0 && y + dy[d] > 0 && x + dx[d] < w + 1 &&
            y + dy[d] < h + 1) {
          if (c == '.' && castle[y + dy[d] - 1][x + dx[d] - 1] == '#')
            n++;
          if (n < count[x + dx[d]][y + dy[d]]) {
            count[x + dx[d]][y + dy[d]] = n;
            st.push((y + dy[d]) * (w + 2) + x + dx[d]);
          }
        } else {
          if (n < min)
            min = n;
        }
      }
    }
    cout << min << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
  int w, h;
  string line;
  while (cin >> w >> h) {
    vector<string> castle;
    for (int i = 0; i < h; i++) {
      cin >> line;
      castle.push_back(line);
    }
    int count[w + 2][h + 2];
    int sx, sy;
    const int MAX = 100;
    for (int y = 1; y < h + 1; y++) {
      for (int x = 1; x < w + 1; x++) {
        count[x][y] = MAX;
        if (castle[y - 1][x - 1] == '&') {
          sx = x;
          sy = y;
          castle[y - 1][x - 1] == '.';
        }
      }
    }
    int min = MAX;
    int dx[4] = {1, 0, -1, 0};
    int dy[4] = {0, 1, 0, -1};
    count[sx][sy] = 0;
    stack<int> st;
    st.push(sy * (w + 2) + sx);
    while (!st.empty()) {
      int x = st.top() % (w + 2);
      int y = st.top() / (w + 2);
      st.pop();
      char c = castle[y - 1][x - 1];
      int n;
      for (int d = 0; d < 4; d++) {
        n = count[x][y];
        if (x + dx[d] > 0 && y + dy[d] > 0 && x + dx[d] < w + 1 &&
            y + dy[d] < h + 1) {
          if (c == '.' && castle[y + dy[d] - 1][x + dx[d] - 1] == '#')
            n++;
          if (n < count[x + dx[d]][y + dy[d]]) {
            count[x + dx[d]][y + dy[d]] = n;
            st.push((y + dy[d]) * (w + 2) + x + dx[d]);
          }
        } else {
          if (n < min)
            min = n;
        }
      }
    }
    cout << min << endl;
  }
  return 0;
}
Problem p00156 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int H, W;
char grid[110][110];

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

int memo[101][101];

inline bool IN(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; }

int const INF = 1 << 29;

int main() {

  while (cin >> W >> H && W) {

    int sx, sy;

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> grid[i][j];
        if (grid[i][j] == '&') {
          sx = j, sy = i;
          grid[i][j] = '.';
        }
      }
    }

    int ans = 1 << 29;

    memset(memo, -1, sizeof(memo));
    typedef pair<int, int> Pii;
    typedef pair<int, Pii> Piii;

    priority_queue<Piii> Q;
    Q.push(Piii(0, Pii(sx, sy)));
    while (!Q.empty()) {
      Piii piii = Q.top();
      Q.pop();
      int const x = piii.second.first;
      int const y = piii.second.second;
      int const cost = -piii.first;

      if (x == 0 || y == 0 || x == W - 1 || y == H - 1) {
        cout << cost << endl;
        break;
      }

      for (int k = 0; k < 4; k++) {
        int nx = x + dx[k], ny = y + dy[k];
        if (IN(nx, ny)) {
          int ncost = cost + (grid[y][x] == '.' && grid[ny][nx] == '#');
          if (memo[ny][nx] == -1 || memo[ny][nx] > ncost) {
            memo[ny][nx] = ncost;
            Q.push(Piii(-ncost, Pii(nx, ny)));
          }
        }
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

int H, W;
char grid[101][101];

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

int memo[101][101];

inline bool IN(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; }

int const INF = 1 << 29;

int main() {

  while (cin >> W >> H && W) {

    int sx, sy;

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> grid[i][j];
        if (grid[i][j] == '&') {
          sx = j, sy = i;
        }
      }
    }

    memset(memo, -1, sizeof(memo));
    typedef pair<int, int> Pii;
    typedef pair<int, Pii> Piii;

    int ans = 1 << 29;

    priority_queue<Piii> Q;
    Q.push(Piii(0, Pii(sx, sy)));
    while (!Q.empty()) {
      Piii piii = Q.top();
      Q.pop();
      int const x = piii.second.first;
      int const y = piii.second.second;
      int const cost = piii.first;

      for (int k = 0; k < 4; k++) {
        int nx = x + dx[k], ny = y + dy[k];
        if (IN(nx, ny)) {
          int ncost = cost + (grid[ny][nx] == '#');
          if (memo[ny][nx] == -1 || memo[ny][nx] > ncost) {
            memo[ny][nx] = ncost;
            Q.push(Piii(ncost, Pii(nx, ny)));
          }
        } else {
          ans = min(ans, cost);
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p00156 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stack>
#include <stdio.h>

using namespace std;

struct da {
  int x, y;
};

int main() {
  int x, y, i, j, c, f1, dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
  char d[101][101], g[2] = {'.', '#'};
  da a, b;
  stack<da> st[2];
  while (cin >> x >> y) {
    if (x == 0)
      break;
    for (i = 0; i < y; i++)
      cin >> d[i];
    c = 0;
    f1 = 0;
    for (i = 0; i < y; i++)
      for (j = 0; j < x; j++)
        if (d[i][j] == '&') {
          a.x = j;
          a.y = i;
          st[0].push(a);
        }
    while (f1 == 0) {
      while (!st[c % 2].empty()) {
        a = st[c % 2].top();
        st[c % 2].pop();
        if (a.x == 0 || a.x == x - 1 || a.y == 0 || a.y == y - 1) {
          f1 = 1;
          continue;
        }
        for (i = 0; i < 4; i++) {
          b.x = a.x + dx[i];
          b.y = a.y + dy[i];
          if (d[b.y][b.x] == g[c % 2]) {
            st[c % 2].push(b);
            d[b.y][a.x] = '&';
          }
          if (d[b.y][b.x] == g[(c + 1) % 2]) {
            st[(c + 1) % 2].push(b);
            d[b.y][a.x] = '&';
          }
        }
      }
      c++;
    }
    while (!st[c % 2].empty())
      st[c % 2].pop();
    cout << c / 2 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>

using namespace std;

int main() {
  int x, y, i, j, k, c, f1, f2, dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
  char d[101][101], g[2] = {'.', '#'};
  while (cin >> x >> y) {
    if (x == 0)
      break;
    for (i = 0; i < y; i++)
      cin >> d[i];
    c = 0;
    while (true) {
      f1 = 0;
      f2 = 0;
      while (f1 + f2 == 0) {
        f1 = 1;
        for (i = 0; i < y; i++)
          for (j = 0; j < x; j++) {
            if (d[i][j] == '&') {
              for (k = 0; k < 4; k++) {
                if (d[i + dy[k]][j + dx[k]] == g[c % 2]) {
                  f1 = 0;
                  d[i + dy[k]][j + dx[k]] = '&';
                  if (i + dy[k] == 0 || i + dy[k] == y - 1 || j + dx[k] == 0 ||
                      j + dx[k] == x - 1)
                    f2 = 1;
                }
              }
            }
          }
      }
      if (f2 == 1)
        break;
      c++;
    }
    cout << (c + 1) / 2 << endl;
  }
  return 0;
}
Problem p00156 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
#define MAX_N 1000
int X[MAX_N][MAX_N], dist[MAX_N][MAX_N], W, H, gx, gy;
char c;
int main() {
  while (true) {
    for (int i = 0; i < MAX_N; i++) {
      for (int j = 0; j < MAX_N; j++) {
        X[i][j] = 1000000000;
        dist[i][j] = 1000000000;
      }
    }
    cin >> W >> H;
    queue<pair<int, int>> Q;
    if (W == 0 && H == 0) {
      break;
    }
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> c;
        if (c == '&') {
          gx = i;
          gy = j;
        }
        if (c == '#') {
          X[i][j] = 1;
        } else {
          X[i][j] = 0;
        }
        if (i == 1 || i == H || j == 1 || j == W) {
          Q.push(make_pair(i, j));
          dist[i][j] = X[i][j];
        }
      }
    }
    while (!Q.empty()) {
      pair<int, int> pa = Q.front();
      Q.pop();
      int cx = pa.first, cy = pa.second;
      int dx[4] = {1, 0, -1, 0};
      int dy[4] = {0, 1, 0, -1};
      for (int i = 0; i < 4; i++) {
        int ex = cx + dx[i], ey = cy + dy[i];
        if (X[ex][ey] >= 2) {
          continue;
        }
        int dis = dist[cx][cy];
        if (X[cx][cy] == 0 && X[ex][ey] == 1) {
          dis += 1;
        }
        if (dist[ex][ey] > dis) {
          dist[ex][ey] = dis;
          Q.push(make_pair(ex, ey));
        }
      }
    }
    cout << dist[gx][gy] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
#define MAX_N 1000
int X[MAX_N][MAX_N], dist[MAX_N][MAX_N], W, H, gx, gy;
char c;
int main() {
  while (true) {
    for (int i = 0; i < MAX_N; i++) {
      for (int j = 0; j < MAX_N; j++) {
        X[i][j] = 1000000000;
        dist[i][j] = 1000000000;
      }
    }
    cin >> W >> H;
    queue<pair<int, int>> Q;
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> c;
        if (c == '&') {
          gx = i;
          gy = j;
        }
        if (c == '#') {
          X[i][j] = 1;
        } else {
          X[i][j] = 0;
        }
        if (i == 1 || i == H || j == 1 || j == W) {
          Q.push(make_pair(i, j));
          dist[i][j] = X[i][j];
        }
      }
    }
    while (!Q.empty()) {
      pair<int, int> pa = Q.front();
      Q.pop();
      int cx = pa.first, cy = pa.second;
      int dx[4] = {1, 0, -1, 0};
      int dy[4] = {0, 1, 0, -1};
      for (int i = 0; i < 4; i++) {
        int ex = cx + dx[i], ey = cy + dy[i];
        if (X[ex][ey] >= 2) {
          continue;
        }
        int dis = dist[cx][cy];
        if (X[cx][cy] == 0 && X[ex][ey] == 1) {
          dis += 1;
        }
        if (dist[ex][ey] > dis) {
          dist[ex][ey] = dis;
          Q.push(make_pair(ex, ey));
        }
      }
    }
    cout << dist[gx][gy] << endl;
  }
  return 0;
}
Problem p00157 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

typedef std::pair<int, int> P;

int dp[200]; // 何番目のまとりょーしかが一番大きいか＝数

int main() {
  int ichiro, jiro, M = 0;
  while (true) {
    P doll[200]; // 高さ　半径
    std::cin >> ichiro;
    if (!ichiro) {
      break;
    }
    for (int i = 0; i < ichiro; i++) {
      std::cin >> doll[i].first >> doll[i].second;
    }
    std::cin >> jiro;
    for (int i = 0; i < jiro; i++) {
      std::cin >> doll[i + ichiro].first >> doll[i + ichiro].second;
    }
    std::sort(doll, doll + ichiro + jiro);

    for (int i = 0; i < ichiro + jiro; i++)
      dp[i] = 1;
    for (int i = 1; i < ichiro + jiro; i++) {
      for (int j = i - 1; j >= 0; j--) {
        if (doll[i].first != doll[j].first && doll[i].second > doll[j].second) {
          dp[i] = std::max(dp[i], dp[j] + 1);
        }
      }
      M = std::max(M, dp[i]);
    }
    std::cout << M << std::endl;
    M = 0;
    for (int i = 0; i < ichiro + jiro; i++)
      dp[i] = 0;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

typedef std::pair<int, int> P;

int dp[200]; // 何番目のまとりょーしかが一番大きいか＝数

int main() {
  int ichiro, jiro, M = 0;
  while (std::cin >> ichiro, ichiro) {
    P dool[200]; // 高さ　半径
    std::cin >> ichiro;
    for (int i = 0; i < ichiro; i++) {
      std::cin >> dool[i].first >> dool[i].second;
    }
    std::cin >> jiro;
    for (int i = 0; i < jiro; i++) {
      std::cin >> dool[i + ichiro].first >> dool[i + ichiro].second;
    }
    std::sort(dool, dool + ichiro + jiro);
    for (int i = 0; i < ichiro + jiro; i++)
      dp[i] = 1;
    for (int i = 1; i < ichiro + jiro; i++) {
      for (int j = i - 1; j >= 0; j--) {
        dp[i] = std::max(dp[i], dp[j] + (dool[i].second > dool[j].second));
      }
      M = std::max(M, dp[i]);
    }
    std::cout << M << std::endl;
    M = 0;
    for (int i = 0; i < ichiro + jiro; i++)
      dp[i] = 0;
  }
  return 0;
}
Problem p00157 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int dy[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int INF = 1 << 30;
const double EPS = 1e-15;
#define PB push_back
#define mk make_pair
#define fi first
#define se second
#define ll long long
#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
#define MOD 1000000007
typedef pair<int, int> Pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
vector<Pii> data;
int N, M;
int memo[1280][1280];
int solve(int dep, int prev1, int prev2) {
  if (dep == N + M) {
    return 0;
  }
  if (memo[prev1][prev2])
    return memo[prev1][prev2];
  int ret = 0;
  reps(i, dep, N + M) {
    if (prev1 < data[i].fi && prev2 < data[i].se) {
      ret = max(ret, 1 + solve(i + 1, data[i].fi, data[i].se));
    }
  }
  return memo[prev1][prev2] = ret;
}
int main() {
  while (scanf("%d", &N), N) {
    data.clear();
    memset(memo, 0, sizeof(memo));
    rep(i, N) {
      int a, b;
      scanf("%d%d", &a, &b);
      data.PB(Pii(a, b));
    }
    scanf("%d", &M);
    rep(i, M) {
      int a, b;
      scanf("%d%d", &a, &b);
      data.PB(Pii(a, b));
    }
    sort(data.begin(), data.end());
    printf("%d\\n", solve(0, 0, 0));
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int dy[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int INF = 1 << 30;
const double EPS = 1e-15;
#define PB push_back
#define mk make_pair
#define fi first
#define se second
#define ll long long
#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
#define MOD 1000000007
typedef pair<int, int> Pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
vector<Pii> data;
int N, M;
int memo[2][1280][1280];
int solve(int dep, int prev1, int prev2) {
  if (dep == N + M) {
    return 0;
  }
  if (memo[dep % 2][prev1][prev2])
    return memo[dep % 2][prev1][prev2];
  int ret = 0;
  reps(i, dep, N + M) {
    if (prev1 < data[i].fi && prev2 < data[i].se) {
      ret = max(ret, max(1 + solve(i + 1, data[i].fi, data[i].se),
                         solve(i + 1, prev1, prev2)));
    } else {
      ret = max(ret, solve(i + 1, prev1, prev2));
    }
  }
  return memo[dep % 2][prev1][prev2] = ret;
}
int main() {
  while (scanf("%d", &N), N) {
    data.clear();
    memset(memo, 0, sizeof(memo));
    rep(i, N) {
      int a, b;
      scanf("%d%d", &a, &b);
      data.PB(Pii(a, b));
    }
    scanf("%d", &M);
    rep(i, M) {
      int a, b;
      scanf("%d%d", &a, &b);
      data.PB(Pii(a, b));
    }
    sort(data.begin(), data.end());
    printf("%d\\n", solve(0, 0, 0));
  }

  return 0;
}
Problem p00157 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF 1e8
#define EPS 1e-9

#define rep2(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) rep2(i, 0, n)
#define ll long long
#define P pair<int, int>
#define pb push_back

int n, m, h[200], r[200], d[200][200];

int main() {
  while (cin >> n && n) {
    rep(i, n) cin >> h[i] >> r[i];
    cin >> m;
    rep(i, m) cin >> h[i + n] >> r[i + n];
    n += m;
    rep(i, n) fill(d[i], d[i] + n, INF);
    rep(i, n) rep(j, n) {
      if (i == j)
        d[i][j] = 0;
      if (h[i] > h[j] && r[i] > r[j])
        d[i][j] = -1;
    }
    rep(k, n) rep(i, n) rep(j, n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
    int ans = 0;
    rep(i, n) rep(j, n) ans = max(ans, -d[i][j] + 1);
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF 1e8
#define EPS 1e-9

#define rep2(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) rep2(i, 0, n)
#define ll long long
#define P pair<int, int>
#define pb push_back

int n, m, h[200], r[200], d[200][200];

int main() {
  while (cin >> n && n) {
    rep(i, n) cin >> h[i] >> r[i];
    cin >> m;
    rep(i, m) cin >> h[i + n] >> r[i + n];
    n += m;
    rep(i, n + m) fill(d[i], d[i] + n + m, INF);
    rep(i, n) rep(j, n) {
      if (i == j)
        d[i][j] = 0;
      if (h[i] > h[j] && r[i] > r[j])
        d[i][j] = -1;
    }
    rep(k, n) rep(i, n) rep(j, n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
    int ans = 0;
    rep(i, n) rep(j, n) ans = max(ans, -d[i][j] + 1);
    cout << ans << endl;
  }
}