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

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

int main() {
  int n, m, x, y, rx, ry, l, cnt, d[N][N];
  char c;
  while (1) {
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        d[i][j] = 0;
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++)
      cin >> x >> y, d[N - y - 1][x] = 1;
    cin >> m;
    rx = ry = 10;
    cnt = 0;
    while (m--) {
      cin >> c >> l;
      while (l--) {
        if (c == 'N')
          ry--;
        if (c == 'E')
          rx++;
        if (c == 'S')
          ry++;
        if (c == 'W')
          rx++;
        if (d[ry][rx])
          d[ry][rx] = 0, cnt++;
      }
    }
    if (cnt == n)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

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

int main() {
  int n, m, x, y, rx, ry, l, cnt, d[N][N];
  char c;
  while (1) {
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        d[i][j] = 0;
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++)
      cin >> x >> y, d[N - y - 1][x] = 1;
    cin >> m;
    rx = ry = 10;
    cnt = 0;
    while (m--) {
      cin >> c >> l;
      while (l--) {
        if (c == 'N')
          ry--;
        if (c == 'E')
          rx++;
        if (c == 'S')
          ry++;
        if (c == 'W')
          rx--;
        if (d[ry][rx])
          d[ry][rx] = 0, cnt++;
      }
    }
    if (cnt == n)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}
Problem p01125 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>

#define MAX 32

using namespace std;

int main(void) {
  while (1) {
    int n, m;
    int x[MAX], y[MAX];
    int m_x = 10, m_y = 10;
    char d;
    int l;
    bool moved[MAX][MAX] = {false};
    moved[10][10] = true;
    bool collect = true;

    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d %d", &x[i], &y[i]);
    }
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
      cin >> d >> l;
      if (d == 'N') {
        for (int j = 0; j <= l; j++) {
          moved[m_x][m_y + j] = true;
        }
        m_y += l;
      }
      if (d == 'E') {
        for (int j = 0; j <= l; j++) {
          moved[m_x + j][m_y] = true;
        }
        m_x += l;
      }
      if (d == 'S') {
        for (int j = 0; j <= l; j++) {
          moved[m_x][m_y - j] = true;
        }
        m_y -= l;
      }
      if (d == 'W') {
        for (int j = 0; j <= l; j++) {
          moved[m_x - j][m_y] = true;
        }
        m_y -= l;
      }
    }

    for (int i = 0; i < n; i++) {
      if (!moved[x[i]][y[i]])
        collect = false;
    }

    printf("%s\n", collect ? "Yes" : "No");
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>

#define MAX 32

using namespace std;

int main(void) {
  while (1) {
    int n, m;
    int x[MAX], y[MAX];
    int m_x = 10, m_y = 10;
    char d;
    int l;
    bool moved[MAX][MAX] = {false};
    moved[10][10] = true;
    bool collect = true;

    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d %d", &x[i], &y[i]);
    }
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
      cin >> d >> l;
      if (d == 'N') {
        for (int j = 0; j <= l; j++) {
          moved[m_x][m_y + j] = true;
        }
        m_y += l;
      }
      if (d == 'E') {
        for (int j = 0; j <= l; j++) {
          moved[m_x + j][m_y] = true;
        }
        m_x += l;
      }
      if (d == 'S') {
        for (int j = 0; j <= l; j++) {
          moved[m_x][m_y - j] = true;
        }
        m_y -= l;
      }
      if (d == 'W') {
        for (int j = 0; j <= l; j++) {
          moved[m_x - j][m_y] = true;
        }
        m_x -= l;
      }
    }

    for (int i = 0; i < n; i++) {
      if (!moved[x[i]][y[i]])
        collect = false;
    }

    printf("%s\n", collect ? "Yes" : "No");
  }
  return 0;
}
Problem p01126 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <map>

using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;

int main() {
  while (true) {
    int n, m, a;
    scanf("%d%d%d", &n, &m, &a);
    if (n == 0 || m == 0 || a == 0) {
      break;
    }
    multimap<int, pii, greater<int>> field;
    for (int i = 0; i < m; i++) {
      int h, p, q;
      scanf("%d%d%d", &h, &p, &q);
      field.insert(pipii(h, pii(p, q)));
    }
    for (multimap<int, pii, greater<int>>::iterator it = field.begin();
         it != field.end(); it++) {
      pii item = (*it).second;
      if (a == item.first) {
        a = item.second;
      } else if (a == item.second) {
        a = item.first;
      }
    }
    printf("%d\n", a);
  }
  return true;
}

Fixed submission:
#include <cstdio>
#include <map>

using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;

int main() {
  while (true) {
    int n, m, a;
    scanf("%d%d%d", &n, &m, &a);
    if (n == 0 || m == 0 || a == 0) {
      break;
    }
    multimap<int, pii, greater<int>> field;
    for (int i = 0; i < m; i++) {
      int h, p, q;
      scanf("%d%d%d", &h, &p, &q);
      field.insert(pipii(h, pii(p, q)));
    }
    for (multimap<int, pii, greater<int>>::iterator it = field.begin();
         it != field.end(); it++) {
      pii item = (*it).second;
      if (a == item.first) {
        a = item.second;
      } else if (a == item.second) {
        a = item.first;
      }
    }
    printf("%d\n", a);
  }
  return 0;
}
Problem p01126 (C++)

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

#define mp make_pair
#define pb push_back
#define REP(i, a, n) for (int i = a; i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(s) s.begin(), s.end()
#define rall(s) s.rbegin(), s.rend()
#define range(x, min, max) ((min) <= (x) && (x) <= (max))
#define xyrange(x, minX, maxX, y, minY, maxY)                                  \
  (range(x, minX, maxX) && range(y, minY, maxY))

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef pair<int, int> PII;
typedef vector<PII> VPII;

const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

int n, m, a;
VVI tbl;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (1) {
    tbl.clear();
    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0)
      break;
    tbl.resize(n + 1, VI(1001));
    rep(i, m) {
      int h, p, q;
      cin >> h >> p >> q;
      tbl[h][p] = q;
      tbl[h][q] = p;
    }
    int pos = a;
    rep(i, 1001) {
      if (tbl[pos][i] != 0) {
        pos = tbl[pos][i];
      }
    }
    cout << pos << endl;
  }
  return 0;
}

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

#define mp make_pair
#define pb push_back
#define REP(i, a, n) for (int i = a; i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(s) s.begin(), s.end()
#define rall(s) s.rbegin(), s.rend()
#define range(x, min, max) ((min) <= (x) && (x) <= (max))
#define xyrange(x, minX, maxX, y, minY, maxY)                                  \
  (range(x, minX, maxX) && range(y, minY, maxY))

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef pair<int, int> PII;
typedef vector<PII> VPII;

const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

int n, m, a;
VVI tbl;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (1) {
    tbl.clear();
    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0)
      break;
    tbl.resize(n + 1, VI(1001));
    rep(i, m) {
      int h, p, q;
      cin >> h >> p >> q;
      tbl[p][1000 - h] = q;
      tbl[q][1000 - h] = p;
    }
    int pos = a;
    rep(i, 1001) {
      if (tbl[pos][i] != 0) {
        pos = tbl[pos][i];
      }
    }
    cout << pos << endl;
  }
  return 0;
}
Problem p01126 (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;
typedef pair<int, int> P;

struct st {
  int a, b, c;
};
int main() {
  int n, m, a;
  while (cin >> n >> m >> a, n) {
    vector<st> v;
    rep(i, n) {
      int h, p, q;
      cin >> h >> p >> q;
      v.push_back({h, p, q});
    }
    sort(v.begin(), v.end(), [](st a, st b) { return a.a > b.a; });
    int pos = a;
    for (st i : v) {
      if (i.b == pos)
        pos = i.c;
      else if (i.c == pos)
        pos = i.b;
    }
    cout << pos << endl;
  }
}

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

struct st {
  int a, b, c;
};
int main() {
  int n, m, a;
  while (cin >> n >> m >> a, n) {
    vector<st> v;
    rep(i, m) {
      int h, p, q;
      cin >> h >> p >> q;
      v.push_back({h, p, q});
    }
    sort(v.begin(), v.end(), [](st a, st b) { return a.a > b.a; });
    int pos = a;
    for (st i : v) {
      if (i.b == pos)
        pos = i.c;
      else if (i.c == pos)
        pos = i.b;
    }
    cout << pos << endl;
  }
}
Problem p01126 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
int main() {

  int n, m, a;
  // a,bÌcüÉ©©é¡üÌ³Ìê
  set<int> rightLine[101];
  set<int> leftLine[101];
  while (cin >> n >> m >> a && !(n == 0 && m == 0 && a == 0)) {
    for (int i = 0; i < 101; i++) {
      rightLine[i].clear();
      leftLine[i].clear();
    }
    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      rightLine[p].insert(-h);
      leftLine[q].insert(-h);
    }
    int ch = -1002;
    int ct = a;
    // S[·éÜÅV~[g
    while (1) {
      int t = 1;
      int s = 1;
      // ©gÌEÆ¶ð`FbN
      if (ct != 1) {
        set<int>::iterator it = leftLine[ct].upper_bound(ch);
        if (it != leftLine[ct].end())
          t = *it;
      }
      if (ct != n) {
        set<int>::iterator it = rightLine[ct].upper_bound(ch);
        if (it != rightLine[ct].end())
          s = *it;
      }
      if (t == 1 && s == 1)
        break;
      else if (t == 1) {
        ch = s;
        ct++;
      } else if (s == 1) {
        ch = t;
        ct--;
      }
    }
    cout << ct << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
int main() {

  int n, m, a;
  // a,bÌcüÉ©©é¡üÌ³Ìê
  set<int> rightLine[101];
  set<int> leftLine[101];
  while (cin >> n >> m >> a && !(n == 0 && m == 0 && a == 0)) {
    for (int i = 0; i < 101; i++) {
      rightLine[i].clear();
      leftLine[i].clear();
    }
    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      rightLine[p].insert(-h);
      leftLine[q].insert(-h);
    }
    int ch = -1002;
    int ct = a;
    // S[·éÜÅV~[g
    while (1) {
      int t = 1;
      int s = 1;
      // ©gÌEÆ¶ð`FbN
      if (ct != 1) {
        set<int>::iterator it = leftLine[ct].upper_bound(ch);
        if (it != leftLine[ct].end())
          t = *it;
      }
      if (ct != n) {
        set<int>::iterator it = rightLine[ct].upper_bound(ch);
        if (it != rightLine[ct].end())
          s = *it;
      }
      if (t == 1 && s == 1)
        break;
      else if (t == 1) {
        ch = s;
        ct++;
      } else if (s == 1) {
        ch = t;
        ct--;
      } else {
        if (s > t) {
          ch = t;
          ct--;
        } else {
          ch = s;
          ct++;
        }
      }
    }
    cout << ct << endl;
  }

  return 0;
}
Problem p01126 (C++)

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

using namespace std;

int main() {
  int n, m, a;

  while (true) {
    cin >> n >> m >> a;

    vector<int> amida1(m), amida2(m), amida3(m);

    for (int i = 0; i < m; i++) {
      cin >> amida1[i] >> amida2[i] >> amida3[i];
    }

    int maxh, maxpos, pos = a, posh = 869120;

    while (true) {
      maxh = -1, maxpos = -1;

      for (int i = 0; i < m; i++) {
        if (amida2[i] == pos) {
          if (maxh < amida1[i] && amida1[i] < posh) {
            maxh = amida1[i];
            maxpos = amida3[i];
          }
        }

        if (amida3[i] == pos) {
          if (maxh < amida1[i] && amida1[i] < posh) {
            maxh = amida1[i];
            maxpos = amida2[i];
          }
        }
      }

      if (maxh == -1) {
        break;
      }

      pos = maxpos;
      posh = maxh;
    }

    cout << pos << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m, a;

  while (true) {
    cin >> n >> m >> a;

    if (n == 0 && m == 0 && a == 0) {
      break;
    }

    vector<int> amida1(m), amida2(m), amida3(m);

    for (int i = 0; i < m; i++) {
      cin >> amida1[i] >> amida2[i] >> amida3[i];
    }

    int maxh, maxpos, pos = a, posh = 869120;

    while (true) {
      maxh = -1, maxpos = -1;

      for (int i = 0; i < m; i++) {
        if (amida2[i] == pos) {
          if (maxh < amida1[i] && amida1[i] < posh) {
            maxh = amida1[i];
            maxpos = amida3[i];
          }
        }

        if (amida3[i] == pos) {
          if (maxh < amida1[i] && amida1[i] < posh) {
            maxh = amida1[i];
            maxpos = amida2[i];
          }
        }
      }

      if (maxh == -1) {
        break;
      }

      pos = maxpos;
      posh = maxh;
    }

    cout << pos << endl;
  }

  return 0;
}
Problem p01126 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define all(a) a.begin(), a.end()
#define len(x) ((int)(x).size())
#define tmax(a, b, c) max((a), max((b), (c)))
#define tmin(a, b, c) min((a), min((b), (c)))
#define debug(x) cerr << #x << " is " << x << endl;

typedef pair<int, int> Pii;
typedef vector<int> Vi;
typedef vector<vector<int>> VVi;
typedef long long ll;
const int inf = 2e9;
const ll ll_inf = 1e17;
const int mod = 1e9 + 7;
const long double eps = 1e-10;

int amd[110][1010];

int main() {
  while (1) {
    int n, m, a;
    cin >> n >> m >> a;
    if (!n && !m && !a)
      break;
    memset(amd, 0, sizeof(amd));
    int d = 0;
    rep(i, 0, m) {
      int h, p, q;
      cin >> h >> p >> q;
      if (p > q)
        swap(p, q);
      amd[h][p] = 1;
      amd[h][q] = 2;
      d = max(d, h);
    }
    while (d) {
      // printf("n=%d a=%d amd[n][a]=%d\n",n,a,amd[n][a]);
      if (amd[d][a] == 1)
        a++;
      else if (amd[d][a] == 2)
        a--;
      d--;
    }
    cout << a << endl;
  }

  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define all(a) a.begin(), a.end()
#define len(x) ((int)(x).size())
#define tmax(a, b, c) max((a), max((b), (c)))
#define tmin(a, b, c) min((a), min((b), (c)))
#define debug(x) cerr << #x << " is " << x << endl;

typedef pair<int, int> Pii;
typedef vector<int> Vi;
typedef vector<vector<int>> VVi;
typedef long long ll;
const int inf = 2e9;
const ll ll_inf = 1e17;
const int mod = 1e9 + 7;
const long double eps = 1e-10;

int amd[1010][1010];

int main() {
  while (1) {
    int n, m, a;
    cin >> n >> m >> a;
    if (!n && !m && !a)
      break;
    memset(amd, 0, sizeof(amd));
    int d = 0;
    rep(i, 0, m) {
      int h, p, q;
      cin >> h >> p >> q;
      if (p > q)
        swap(p, q);
      amd[h][p] = 1;
      amd[h][q] = 2;
      d = max(d, h);
    }
    while (d) {
      // printf("n=%d a=%d amd[n][a]=%d\n",n,a,amd[n][a]);
      if (amd[d][a] == 1)
        a++;
      else if (amd[d][a] == 2)
        a--;
      d--;
    }
    cout << a << endl;
  }

  return 0;
}
Problem p01126 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define int long long

vector<vector<int>> w_line;

signed main() {
  int n, m, a;
  while (cin >> n >> m >> a, n) {
    map<int, int> w_line[1001];
    for (int i = 0; i < n; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      w_line[h][p] = q;
      w_line[h][q] = p;
    }

    for (int i = 1000; i >= 1; i--) {
      if (w_line[i][a])
        a = w_line[i][a];
    }

    cout << a << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int long long

vector<vector<int>> w_line;

signed main() {
  int n, m, a;
  while (cin >> n >> m >> a, n) {
    int w_line[1001][101] = {0};
    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      w_line[h][p] = q;
      w_line[h][q] = p;
    }

    for (int i = 1000; i >= 1; i--) {
      if (w_line[i][a])
        a = w_line[i][a];
    }

    cout << a << endl;
  }
  return 0;
}
Problem p01126 (C++)

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

struct ans {
  int n;
  ans *next;
};

int main() {
  //
  ans *start = new ans();
  start->next = NULL;
  ans *end = start, *buf;
  //
  char s[20];
  char *ps;
  int i, j;
  int n, m, a;
  int h, p, q;
  int line[100][1000];
  int x, y;
  while (fgets(s, sizeof(s), stdin)) {
    ps = strtok(s, " ");
    n = atoi(ps);
    ps = strtok(NULL, " ");
    m = atoi(ps);
    ps = strtok(NULL, " ");
    a = atoi(ps) - 1;
    if (n == 0 && m == 0 && a == -1)
      break;
    memset(line, 0, sizeof(line));
    for (i = 0; i < m; i++) {
      fgets(s, sizeof(s), stdin);
      ps = strtok(s, " ");
      h = atoi(ps) - 1;
      ps = strtok(NULL, " ");
      p = atoi(ps) - 1;
      ps = strtok(NULL, " ");
      q = atoi(ps) - 1;

      line[p][h] = (p - q);
      line[q][h] = (q - p);
    }
    x = a;
    for (y = 999; y >= 0; y--)
      x += line[x][y];
    //
    buf = new ans();
    buf->n = x + 1;
    buf->next = NULL;
    end->next = buf;
    end = buf;
    //
  }
  buf = start->next;
  delete start;
  while (buf != NULL) {
    cout << (buf->n) << endl;
    end = buf;
    buf = buf->next;
    delete end;
  }
  return 0;
}

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

struct ans {
  int n;
  ans *next;
};

int main() {
  //
  ans *start = new ans();
  start->next = NULL;
  ans *end = start, *buf;
  //
  char s[20];
  char *ps;
  int i, j;
  int n, m, a;
  int h, p, q;
  int line[100][1000];
  int x, y;
  while (fgets(s, sizeof(s), stdin)) {
    ps = strtok(s, " ");
    n = atoi(ps);
    ps = strtok(NULL, " ");
    m = atoi(ps);
    ps = strtok(NULL, " ");
    a = atoi(ps) - 1;
    if (n == 0 && m == 0 && a == -1)
      break;
    memset(line, 0, sizeof(line));
    for (i = 0; i < m; i++) {
      fgets(s, sizeof(s), stdin);
      ps = strtok(s, " ");
      h = atoi(ps) - 1;
      ps = strtok(NULL, " ");
      p = atoi(ps) - 1;
      ps = strtok(NULL, " ");
      q = atoi(ps) - 1;

      line[p][h] = (q - p);
      line[q][h] = (p - q);
    }
    x = a;
    for (y = 999; y >= 0; y--)
      x += line[x][y];
    //
    buf = new ans();
    buf->n = x + 1;
    buf->next = NULL;
    end->next = buf;
    end = buf;
    //
  }
  buf = start->next;
  delete start;
  while (buf != NULL) {
    cout << (buf->n) << endl;
    end = buf;
    buf = buf->next;
    delete end;
  }
  return 0;
}
Problem p01126 (C++)

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

int t[1002][1002];

int main() {
  int n, m, a;

  while (cin >> n >> m >> a, n || m || a) {
    memset(t, -1, sizeof(t));

    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      t[1000 - h - 1][p - 1] = q - 1;
      t[1000 - h - 1][q - 1] = p - 1;
    }

    a--;

    for (int i = 0; i < 1000; i++) {
      if (t[i][a] == -1)
        continue;
      a = t[i][a];
    }

    cout << a + 1 << endl;
  }
}

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

int t[1002][1002];

int main() {
  int n, m, a;

  while (cin >> n >> m >> a, n || m || a) {
    memset(t, -1, sizeof(t));

    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      t[1000 - h][p - 1] = q - 1;
      t[1000 - h][q - 1] = p - 1;
    }

    a--;

    for (int i = 0; i < 1000; i++) {
      if (t[i][a] == -1)
        continue;
      a = t[i][a];
    }

    cout << a + 1 << endl;
  }
}
Problem p01126 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <utility>

using namespace std;

int main() {
  int n, m, a;
  while (1) {
    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0)
      break;
    int amida[1000][100] = {0};
    int h, p, q, max_h = 0;
    for (int i = 0; i < m; i++) {
      cin >> h >> p >> q;
      amida[h][p] = 1;
      amida[h][q] = -1;
      if (h > max_h)
        max_h = h;
    }
    pair<int, int> dot;
    dot.first = max_h + 1;
    dot.second = a;
    while (1) {
      if (dot.first == 0)
        break;
      else if (amida[dot.first][dot.second] == 1) {
        (dot.second)++;
        amida[dot.first][dot.second] = 0;
      } else if (amida[dot.first][dot.second] == -1) {
        (dot.second)--;
        amida[dot.first][dot.second] = 0;
      } else {
        (dot.first)--;
      }
    }
    cout << dot.second << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <utility>

using namespace std;

int main() {
  int n, m, a;
  while (1) {
    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0)
      break;
    int amida[1001][101] = {0};
    int h, p, q, max_h = 0;
    for (int i = 0; i < m; i++) {
      cin >> h >> p >> q;
      amida[h][p] = 1;
      amida[h][q] = -1;
      if (h > max_h)
        max_h = h;
    }
    pair<int, int> dot;
    dot.first = max_h + 1;
    dot.second = a;
    while (1) {
      if (dot.first == 0)
        break;
      else if (amida[dot.first][dot.second] == 1) {
        (dot.second)++;
        amida[dot.first][dot.second] = 0;
      } else if (amida[dot.first][dot.second] == -1) {
        (dot.second)--;
        amida[dot.first][dot.second] = 0;
      } else {
        (dot.first)--;
      }
    }
    cout << dot.second << endl;
  }
  return 0;
}
Problem p01126 (C++)

Buggy submission (status: Runtime Error):

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2001

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \
  cout << "{ ";                                                                \
  for (auto nth : list) {                                                      \
    cout << nth << " ";                                                        \
  }                                                                            \
  cout << "}" << endl;

using namespace std;

struct Line {
  int height;
  int from;
  int to;

  bool operator<(const Line &right) const { return height < right.height; }
};

int main() {
  int N, M, A;

  while (cin >> N >> M >> A) {

    if (N == 0 && M == 0 && A == 0) {
      break;
    }

    vector<Line> lines(M);

    vector<int> amida(N + 1, 0);
    REP(i, 1, N + 2) { amida[i] = i; }

    if (M != 0) {
      int H, P, Q;
      rep(i, M) {
        cin >> H >> P >> Q;
        lines[i] = {H, P, Q};
      }

      sort(ALL(lines));

      rep(i, M) {
        int p = lines[i].from;
        int q = lines[i].to;
        swap(amida[p], amida[q]);
      }
    }
    cout << amida[A] << endl;
  }

  return 0;
}

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2001

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \
  cout << "{ ";                                                                \
  for (auto nth : list) {                                                      \
    cout << nth << " ";                                                        \
  }                                                                            \
  cout << "}" << endl;

using namespace std;

struct Line {
  int height;
  int from;
  int to;

  bool operator<(const Line &right) const { return height < right.height; }
};

int main() {
  int N, M, A;

  while (cin >> N >> M >> A) {

    if (N == 0 && M == 0 && A == 0) {
      break;
    }

    vector<Line> lines(M);

    vector<int> amida(N + 1, 0);
    rep(i, amida.size()) { amida[i] = i; }

    if (M != 0) {
      int H, P, Q;
      rep(i, M) {
        cin >> H >> P >> Q;
        lines[i] = {H, P, Q};
      }

      sort(ALL(lines));

      rep(i, M) {
        int p = lines[i].from;
        int q = lines[i].to;
        swap(amida[p], amida[q]);
      }
    }
    cout << amida[A] << endl;
  }

  return 0;
}
Problem p01126 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int main() {

  int n, m, a;
  while (cin >> n >> m >> a, n) {
    vector<vector<pair<int, int>>> line(1001);
    for (int i = 0; i < n; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      line[h].push_back(make_pair(p, q));
      line[h].push_back(make_pair(q, p));
    }

    int now = a;
    for (int i = 1000; i > 0; i--) {
      for (int j = 0; j < line[i].size(); j++) {
        if (now == line[i][j].first) {
          now = line[i][j].second;
          break;
        }
      }
    }
    cout << now << endl;
  }
}

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

int main() {

  int n, m, a;
  while (cin >> n >> m >> a, n) {
    vector<vector<pair<int, int>>> line(1001);
    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      line[h].push_back(make_pair(p, q));
      line[h].push_back(make_pair(q, p));
    }

    int now = a;
    for (int i = 1000; i > 0; i--) {
      for (int j = 0; j < line[i].size(); j++) {
        if (now == line[i][j].first) {
          now = line[i][j].second;
          break;
        }
      }
    }
    cout << now << endl;
  }
}
Problem p01126 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

class position {
public:
  int x, y;
  int height;
};

int main() {
  int n, m, a;
  while (1) {
    cin >> n >> m >> a;
    if (!n && !m && !a)
      break;
    int dt[n + 1];
    rep(i, n + 1) dt[i] = i;
    position p[m];
    rep(i, m) cin >> p[i].height >> p[i].x >> p[i].y;
    for (int i = 1; i < n - 1; i++) {
      for (int j = n - 1; j >= i; j--) {
        if (p[j].height < p[j - 1].height) {
          position alt = p[j];
          p[j] = p[j - 1];
          p[j - 1] = alt;
        }
      }
    }
    rep(i, m) {
      int alt = dt[p[i].x];
      dt[p[i].x] = dt[p[i].y];
      dt[p[i].y] = alt;
    }
    cout << dt[a] << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

class position {
public:
  int x, y;
  int height;
};

int main() {
  int n, m, a;
  while (1) {
    cin >> n >> m >> a;
    if (!n && !m && !a)
      break;
    int dt[n + 1];
    rep(i, n + 1) dt[i] = i;
    position p[m];
    rep(i, m) cin >> p[i].height >> p[i].x >> p[i].y;
    for (int i = 1; i < m - 1; i++) {
      for (int j = m - 1; j >= i; j--) {
        if (p[j].height < p[j - 1].height) {
          position alt = p[j];
          p[j] = p[j - 1];
          p[j - 1] = alt;
        }
      }
    }
    rep(i, m) {
      int alt = dt[p[i].x];
      dt[p[i].x] = dt[p[i].y];
      dt[p[i].y] = alt;
    }
    cout << dt[a] << endl;
  }
  return 0;
}
Problem p01126 (C++)

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

int main() {
  int field[1001][101];
  while (true) {
    int n, m, a;
    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0) {
      break;
    }
    memset(field, 0, sizeof(field));
    int st = 0;
    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      field[h][p] = q;
      field[h][q] = p;
      st = max(st, h);
    }
    for (int i = st + 1; i > 0; i--) {
      if (field[i][a] != 0) {
        a = field[i][a];
      }
    }
    printf("%d\n", a);
  }
  return 0;
}

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

int main() {
  int field[1001][101];
  while (true) {
    int n, m, a;
    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0) {
      break;
    }
    memset(field, 0, sizeof(field));
    int st = 0;
    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      field[h][p] = q;
      field[h][q] = p;
      st = max(st, h);
    }
    for (int i = st; i > 0; i--) {
      if (field[i][a] != 0) {
        a = field[i][a];
      }
    }
    printf("%d\n", a);
  }
  return 0;
}
Problem p01126 (C++)

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

int main() {
  int n, m, a, h, p, q;
  while (cin >> n >> m >> a, n) {
    vector<pair<int, int>> v;
    vector<vector<pair<int, int>>> vec;
    for (int i = 0; i < n + 2; i++)
      vec.push_back(v);
    for (int i = 0; i < m; i++) {
      cin >> h >> p >> q;
      vec[p].push_back(make_pair(h, q));
      vec[q].push_back(make_pair(h, p));
    }
    for (int i = 0; i < n + 2; i++)
      sort(vec[i].begin(), vec[i].end());
    int mh = 1001;
    while (mh > 0) {
      while (vec[a][vec[a].size() - 1].first < mh)
        mh--;
      for (long i = vec[a].size() - 1; i >= 0; i--) {
        if (vec[a][i].first == mh) {
          a = vec[a][i].second;
          break;
        } else if (vec[a][i].first < mh)
          break;
      }
      mh--;
    }
    cout << a << endl;
  }
}

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

int main() {
  int n, m, a, h, p, q;
  while (cin >> n >> m >> a, n) {
    vector<pair<int, int>> v;
    vector<vector<pair<int, int>>> vec;
    for (int i = 0; i < n + 2; i++)
      vec.push_back(v);
    for (int i = 0; i < m; i++) {
      cin >> h >> p >> q;
      vec[p].push_back(make_pair(h, q));
      vec[q].push_back(make_pair(h, p));
    }
    for (int i = 0; i < n + 2; i++)
      sort(vec[i].begin(), vec[i].end());
    int mh = 1001;
    while (mh > 0) {
      for (long i = vec[a].size() - 1; i >= 0; i--) {
        if (vec[a][i].first == mh) {
          a = vec[a][i].second;
          break;
        } else if (vec[a][i].first < mh)
          break;
      }
      mh--;
    }
    cout << a << endl;
  }
}
Problem p01126 (C++)

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

struct line {
  int h, p, q;
  line() {}
  ~line() {}
  bool operator<(const line &b) const { return h < b.h; }
};

int main() {
  int n, m, a;
  while (true) {
    scanf("%d%d%d", &n, &m, &a);
    if ((n | m | a) == 0)
      break;
    vector<int> amd(n);
    for (int i = 0; i < n; i++) {
      amd[i] = i + 1;
    }
    vector<line> ll(n);
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d", &ll[i].h, &ll[i].p, &ll[i].q);
    }
    sort(ll.begin(), ll.end());
    for (int i = 0; i < m; i++) {
      swap(amd[--ll[i].p], amd[--ll[i].q]);
    }
    printf("%d\n", amd[--a]);
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

struct line {
  int h, p, q;
  line() {}
  ~line() {}
  bool operator<(const line &b) const { return h < b.h; }
};

int main() {
  int n, m, a;
  while (true) {
    scanf("%d%d%d", &n, &m, &a);
    if ((n | m | a) == 0)
      break;
    vector<int> amd(n);
    for (int i = 0; i < n; i++) {
      amd[i] = i + 1;
    }
    vector<line> ll(m);
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d", &ll[i].h, &ll[i].p, &ll[i].q);
    }
    sort(ll.begin(), ll.end());
    for (int i = 0; i < m; i++) {
      swap(amd[--ll[i].p], amd[--ll[i].q]);
    }
    printf("%d\n", amd[--a]);
  }

  return 0;
}
Problem p01126 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define mod 1000000007              // 1e9+7
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 201

bool calc() {
  int n, m, a, h, p, q;
  pair<int, pair<int, int>> v[SIZE];

  scanf("%d%d%d", &n, &m, &a);

  if (n == 0)
    return false;

  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &h, &p, &q);
    v[i] = {-h, {p, q}};
  }

  sort(v, v + m);

  for (int i = 0; i < m; i++) {
    if (a == v[i].second.first)
      a = v[i].second.second;
    else if (a == v[i].second.second)
      a = v[i].second.first;
  }

  printf("%d\n", a);

  return true;
}

int main() {

  while (calc())
    ;

  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define mod 1000000007              // 1e9+7
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 1001

bool calc() {
  int n, m, a, h, p, q;
  pair<int, pair<int, int>> v[SIZE];

  scanf("%d%d%d", &n, &m, &a);

  if (n == 0)
    return false;

  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &h, &p, &q);
    v[i] = {-h, {p, q}};
  }

  sort(v, v + m);

  for (int i = 0; i < m; i++) {
    if (a == v[i].second.first)
      a = v[i].second.second;
    else if (a == v[i].second.second)
      a = v[i].second.first;
  }

  printf("%d\n", a);

  return true;
}

int main() {

  while (calc())
    ;

  return 0;
}
Problem p01126 (C++)

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

using namespace std;

int n, m, a;
int h, p, q;
int now_a;
pair<int, pair<int, int>> data[100];

int main() {

  while (1) {
    // Input
    cin >> n >> m >> a;
    now_a = a;
    if (n + m + a == 0)
      break;
    for (int i = 0; i < m; i++) {
      cin >> h >> p >> q;
      data[i] = make_pair(-h, make_pair(p, q));
    }

    // sort
    sort(data, data + m);

    //
    for (int i = 0; i < m; i++) {
      if ((now_a == data[i].second.first) || (now_a == data[i].second.second))
        (now_a == data[i].second.first) ? now_a = data[i].second.second
                                        : now_a = data[i].second.first;
    }

    // Output
    cout << now_a << endl;
  }

  return 0;
}

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

using namespace std;

int n, m, a;
int h, p, q;
int now_a;
pair<int, pair<int, int>> data[1000];

int main() {

  while (1) {
    // Input
    cin >> n >> m >> a;
    now_a = a;
    if (n + m + a == 0)
      break;
    for (int i = 0; i < m; i++) {
      cin >> h >> p >> q;
      data[i] = make_pair(-h, make_pair(p, q));
    }

    // sort
    sort(data, data + m);

    //
    for (int i = 0; i < m; i++) {
      if ((now_a == data[i].second.first) || (now_a == data[i].second.second))
        (now_a == data[i].second.first) ? now_a = data[i].second.second
                                        : now_a = data[i].second.first;
    }

    // Output
    cout << now_a << endl;
  }

  return 0;
}
Problem p01126 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct amida {
  int h, p, q;
};

bool cmp(const amida &x, const amida &y) { return x.h > y.h; }

int main() {
  int n, m, a;
  while (cin >> n >> m >> a && (n || m || a)) {
    vector<amida> b(n);
    for (int i = 0; i < m; ++i)
      cin >> b[i].h >> b[i].p >> b[i].q;
    sort(b.begin(), b.end(), cmp);
    for (int i = 0; i < m; ++i) {
      if (a == b[i].p)
        a = b[i].q;
      else if (a == b[i].q)
        a = b[i].p;
    }
    cout << a << endl;
  }

  return 0;
}


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

using namespace std;

struct amida {
  int h, p, q;
};

bool cmp(const amida &x, const amida &y) { return x.h > y.h; }

int main() {
  int n, m, a;
  while (cin >> n >> m >> a && (n || m || a)) {
    vector<amida> b(m);
    for (int i = 0; i < m; ++i)
      cin >> b[i].h >> b[i].p >> b[i].q;
    sort(b.begin(), b.end(), cmp);
    for (int i = 0; i < m; ++i) {
      if (a == b[i].p)
        a = b[i].q;
      else if (a == b[i].q)
        a = b[i].p;
    }
    cout << a << endl;
  }

  return 0;
}
Problem p01126 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

struct Bridge {
  int p, q;
};

int n, m, a;
vector<vector<Bridge>> bvec;

void solve() {
  for (int i = 1000 - 1; i >= 0; i--) {
    for (int j = 0; j < (int)bvec[i].size(); i++) {
      Bridge br = bvec[i][j];

      if (br.p == a) {
        a = br.q;
        break;
      } else if (br.q == a) {
        a = br.p;
        break;
      }
    }
  }

  cout << a << endl;
}

int main() {
  while (1) {
    cin >> n >> m >> a;
    if (n == 0)
      break;

    bvec.assign(1000, vector<Bridge>());
    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      bvec[h - 1].push_back((Bridge){p, q});
    }

    solve();
  }

  return 0;
}

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

using namespace std;

struct Bridge {
  int p, q;
};

int n, m, a;
vector<vector<Bridge>> bvec;

void solve() {
  for (int i = 1000 - 1; i >= 0; i--) {
    for (int j = 0; j < (int)bvec[i].size(); j++) {
      Bridge br = bvec[i][j];

      if (br.p == a) {
        a = br.q;
        break;
      } else if (br.q == a) {
        a = br.p;
        break;
      }
    }
  }

  cout << a << endl;
}

int main() {
  while (1) {
    cin >> n >> m >> a;
    if (n == 0)
      break;

    bvec.assign(1000, vector<Bridge>());
    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      bvec[h - 1].push_back((Bridge){p, q});
    }

    solve();
  }

  return 0;
}
Problem p01126 (C++)

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

using namespace std;

int main() {
  int n, m, a;
  while (cin >> n >> m >> a, n || m || a) {
    --a;
    int kuji[1000][100];
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 1000; j++)
        kuji[j][i] = i;
    }
    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      --h, --p, --q;
      kuji[h][p] = q;
      kuji[h][q] = p;
    }
    for (int i = 1000; i >= 0; i--) {
      a = kuji[i][a];
    }
    cout << a + 1 << endl;
  }
  return 0;
}

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

using namespace std;

int main() {
  int n, m, a;
  while (cin >> n >> m >> a, n || m || a) {
    --a;
    int kuji[1000][100];
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 1000; j++)
        kuji[j][i] = i;
    }
    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      --h, --p, --q;
      kuji[h][p] = q;
      kuji[h][q] = p;
    }
    for (int i = 999; i >= 0; i--) {
      a = kuji[i][a];
    }
    cout << a + 1 << endl;
  }
  return 0;
}
Problem p01126 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int n, m, a;

int memo[1005][105] = {0};

int main() {
  int b, i, j = 0, h, p, q;
  while (1) {
    cin >> n >> m >> a;
    if (n + m + a == 0)
      break;
    for (b = 0; b < m; b++) {
      cin >> h >> p >> q;
      memo[h][p] = q;
      memo[h][q] = p;
      if (j < h)
        j = h;
    }
    for (i = j; i > 0; i--) {
      // cout <<" "<< i << " "<<memo[i][a] <<" "<<a <<endl;
      if (memo[i][a])
        a = memo[i][a];
    }
    cout << a << endl;
    for (h = 1; h <= j; i++)
      for (i = 1; i <= n; i++)
        memo[h][i] = 0;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int n, m, a;

int memo[1005][105] = {0};

int main() {
  int b, i, j = 0, h, p, q;
  while (1) {
    cin >> n >> m >> a;
    if (n + m + a == 0)
      break;
    for (b = 0; b < m; b++) {
      cin >> h >> p >> q;
      memo[h][p] = q;
      memo[h][q] = p;
      if (j < h)
        j = h;
    }
    for (i = j; i > 0; i--) {
      // cout <<" "<< i << " "<<memo[i][a] <<" "<<a <<endl;
      if (memo[i][a])
        a = memo[i][a];
    }
    cout << a << endl;
    for (h = 1; h <= j; h++)
      for (i = 1; i <= n; i++)
        memo[h][i] = 0;
  }
  return 0;
}
Problem p01127 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;

using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  while (n--) {
    int w, h;
    cin >> w >> h;
    vector<string> B(h);
    loop(h, i) cin >> B[i];

    unordered_set<char> alphs;
    loop(h, i) loop(w, j) {
      char c = B[i][j];
      if (c != '.')
        alphs.insert(c);
    }
    vector<char> perm(all(alphs));
    sort(all(perm));
    int safe = 0;
    do {
      map<char, int> layer = {{'.', 0}};
      loop(perm.size(), i) layer[perm[i]] = i + 1;

      for (char c : perm) {
        int l = 100, r = 0, t = 100, b = 0;
        loop(h, i) loop(w, j) if (B[i][j] == c) {
          l = min(j, l);
          r = max(j, r);
          t = min(i, t);
          b = max(i, b);
        }
        for (int i = t; i <= b; i++) {
          for (int j = l; j <= r; j++) {
            if (layer[B[i][j]] < layer[c])
              goto NG;
          }
        }
      }
      safe = 1;
      break;
    NG:
      continue;
    } while (next_permutation(all(perm)));
    if (safe)
      cout << "SAFE" << endl;
    else
      cout << "SUSPICIOUS" << endl;
  }
  return 0;
}

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

#define loop(n, i) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;

using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  while (n--) {
    int h, w;
    cin >> h >> w;
    vector<string> B(h);
    loop(h, i) cin >> B[i];

    unordered_set<char> alphs;
    loop(h, i) loop(w, j) {
      char c = B[i][j];
      if (c != '.')
        alphs.insert(c);
    }
    vector<char> perm(all(alphs));
    sort(all(perm));
    int safe = 0;
    do {
      map<char, int> layer = {{'.', 0}};
      loop(perm.size(), i) layer[perm[i]] = i + 1;

      for (char c : perm) {
        int l = 100, r = 0, t = 100, b = 0;
        loop(h, i) loop(w, j) if (B[i][j] == c) {
          l = min(j, l);
          r = max(j, r);
          t = min(i, t);
          b = max(i, b);
        }
        for (int i = t; i <= b; i++) {
          for (int j = l; j <= r; j++) {
            if (layer[B[i][j]] < layer[c])
              goto NG;
          }
        }
      }
      safe = 1;
      break;
    NG:
      continue;
    } while (next_permutation(all(perm)));
    if (safe)
      cout << "SAFE" << endl;
    else
      cout << "SUSPICIOUS" << endl;
  }
  return 0;
}
Problem p01130 (C++)

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

#define INF 10000001

#define MAX_SIZE 101
long long g_d[MAX_SIZE][MAX_SIZE];
void warshall_floyd(int v) {
  for (int k = 0; k < v; ++k) {
    for (int i = 0; i < v; ++i) {
      for (int j = 0; j < v; ++j) {
        g_d[i][j] = min(g_d[i][j], g_d[i][k] + g_d[k][j]);
      }
    }
  }
}

int main() {
  freopen("Text.txt", "r", stdin);

  while (true) {
    int n, m, s, g1, g2;
    cin >> n >> m >> s >> g1 >> g2;
    --s;
    --g1;
    --g2;
    if (n == 0)
      break;

    fill(g_d[0], g_d[0] + MAX_SIZE * MAX_SIZE, INF);
    for (int i = 0; i < MAX_SIZE; ++i) {
      g_d[i][i] = 0;
    }

    for (int i = 0; i < m; ++i) {
      int b1, b2;
      cin >> b1 >> b2;
      --b1;
      --b2;
      cin >> g_d[b1][b2];
    }

    warshall_floyd(n);

    long long cost = INF;
    for (int i = 0; i < n; ++i) {
      cost = min(cost, g_d[s][i] + g_d[i][g1] + g_d[i][g2]);
    }

    cout << cost << endl;
  }
}

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

#define INF 10000001

#define MAX_SIZE 101
long long g_d[MAX_SIZE][MAX_SIZE];
void warshall_floyd(int v) {
  for (int k = 0; k < v; ++k) {
    for (int i = 0; i < v; ++i) {
      for (int j = 0; j < v; ++j) {
        g_d[i][j] = min(g_d[i][j], g_d[i][k] + g_d[k][j]);
      }
    }
  }
}

int main() {

  while (true) {
    int n, m, s, g1, g2;
    cin >> n >> m >> s >> g1 >> g2;
    --s;
    --g1;
    --g2;
    if (n == 0)
      break;

    fill(g_d[0], g_d[0] + MAX_SIZE * MAX_SIZE, INF);
    for (int i = 0; i < MAX_SIZE; ++i) {
      g_d[i][i] = 0;
    }

    for (int i = 0; i < m; ++i) {
      int b1, b2;
      cin >> b1 >> b2;
      --b1;
      --b2;
      cin >> g_d[b1][b2];
    }

    warshall_floyd(n);

    long long cost = INF;
    for (int i = 0; i < n; ++i) {
      cost = min(cost, g_d[s][i] + g_d[i][g1] + g_d[i][g2]);
    }

    cout << cost << endl;
  }
}