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

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n, m, a;
  while (cin >> n >> m >> a && n != 0 && m != 0 && a != 0) {
    int line[1000 + 10][100 + 10] = {};
    int h, p, q;
    int max_height = 0;
    for (int i = 0; i < m; i++) {
      cin >> h >> p >> q;
      line[h][p] = 1;
      line[h][q] = -1;
      max_height = max(max_height, h);
    }
    int x = a;
    for (int y = max_height; y > 0; y--) {
      // cout << "x :" << x << " y :" << y << endl;
      x += line[y][x];
    }
    cout << x << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
  int n, m, a;
  while (cin >> n >> m >> a && n != 0 && m != 0 && a != 0) {
    int line[1000 + 10][100 + 10] = {};
    int h, p, q;
    for (int i = 0; i < m; i++) {
      cin >> h >> p >> q;
      line[m - h + 1][p] = 1;
      line[m - h + 1][q] = -1;
    }
    int x = a;
    for (int y = 1; y <= m; y++) {
      // cout << "x :" << x << " y :" << y << endl;
      x += line[y][x];
    }
    cout << x << endl;
  }
  return 0;
}
Problem p01126 (C++) — buggy submission, verdict: 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[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;
}

Error: 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[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;
}
Problem p01126 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

vector<int> ans;

int main() {
  while (1) {
    int n, m, a;
    cin >> n >> m >> a;
    if (n + m + a == 0)
      break;
    int root[1001][101];
    for (int i = 0; i < 1001; i++) {
      for (int j = 0; j < 101; j++) {
        root[i][j] = 0;
      }
    }
    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      root[h][p] = q;
      root[h][q] = p;
    }
    int now = a;
    for (int i = 1000; i > 0; i--) {
      if (root[i][now] != 0)
        now = root[i][now];
    }
    ans.push_back(now);
  }
  for (int i = 0; i < ans.size(); i++)
    cout << ans[i] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

vector<int> ans;

int main() {
  while (1) {
    int n, m, a;
    cin >> n >> m >> a;
    if (n + m + a == 0)
      break;
    int root[1000][100];
    for (int i = 0; i < 1000; i++) {
      for (int j = 0; j < 100; j++) {
        root[i][j] = 0;
      }
    }
    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      root[h][p] = q;
      root[h][q] = p;
    }
    int now = a;
    for (int i = m; i > 0; i--) {
      if (root[i][now] != 0)
        now = root[i][now];
    }
    ans.push_back(now);
  }
  for (int i = 0; i < ans.size(); i++)
    cout << ans[i] << endl;

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

#include <algorithm>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>

#define el endl
#define fd fixed
#define INF INT_MAX / 2 - 1
#define pb push_back

using namespace std;

int main() {
  int n, m, a, h, p, q;
  while (cin >> n >> m >> a, n | m | a) {
    int hor[1000][n + 1];
    for (int i = 0; i < 1000; i++)
      for (int j = 0; j < n + 1; j++)
        hor[i][j] = 0;
    while (m--) {
      cin >> h >> p >> q;
      hor[1000 - h][p] = 1;
    }
    for (int i = 0; i < 1000; i++) {
      if (hor[i][a - 1])
        a--;
      else if (hor[i][a])
        a++;
    }
    cout << a << el;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>

#define el endl
#define fd fixed
#define INF INT_MAX / 2 - 1
#define pb push_back

using namespace std;

int main() {
  int n, m, a, h, p, q;
  int hor[1000][n + 1];
  while (cin >> n >> m >> a, n | m | a) {
    int can[n + 1];
    for (int i = 0; i < n + 1; i++) {
      can[i] = i;
    }
    for (int i = 0; i < 1000; i++) {
      for (int j = 0; j < n + 1; j++) {
        hor[i][j] = 0;
      }
    }
    while (m--) {
      cin >> h >> p >> q;
      hor[1000 - h][p] = 1;
    }
    for (int i = 0; i < 1000; i++) {
      if (hor[i][a - 1])
        a--;
      else if (hor[i][a])
        a++;
    }
    cout << a << el;
  }
}
Problem p01126 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int n, m, a;
  int yoko[3][1000];
  int i, j;

  while (1) {
    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0)
      break;
    for (i = 0; i < m; i++) {
      cin >> yoko[0][i] >> yoko[1][i] >> yoko[2][i];
    }

    for (i = 1000; i > 0; i--)
      for (j = 0; j < m; j++)
        if (yoko[0][j] == i)
          if (yoko[1][j] == a) {
            a = yoko[2][j];
            break;
          } else if (yoko[2][j] == a) {
            a = yoko[1][j];
            break;
          }

    cout << a << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int n, m, a;
  int yoko[3][100];
  int i, j;

  while (1) {
    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0)
      break;
    for (i = 0; i < m; i++) {
      cin >> yoko[0][i] >> yoko[1][i] >> yoko[2][i];
    }

    for (i = m; i > 0; i--)
      for (j = 0; j < m; j++)
        if (yoko[0][j] == i)
          if (yoko[1][j] == a) {
            a = yoko[2][j];
            break;
          } else if (yoko[2][j] == a) {
            a = yoko[1][j];
            break;
          }

    cout << a << endl;
  }
}
Problem p01126 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

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

void swap(int a, int b, int num[105]) {
  int temp = num[a]; // テヲツィツェテァツキツ堙」ツ?ョテ・ツ?ヲテァツ青?
  num[a] = num[b];
  num[b] = temp;
}

void sort(struct line line[1005], int m) {
  if (m == 0)
    return;
  rep(i,
      m) { // テゥツォツ佚」ツ?陛」ツ?古ゥツォツ佚」ツ??ゥツ??」ツ?ォテ・ツ?ヲテァツ青?」ツ?凖」ツつ凝」ツ?淌」ツつ?」ツ??」ツつステ」ツδシテ」ツδ?
    rep(j, m - 1) {
      if (line[j].h < line[j + 1].h) {
        struct line temp = line[j];
        line[j] = line[j + 1];
        line[j + 1] = temp;
      }
    }
  }
}

int main() {
  int n, m, a;
  while (cin >> n >> m >> a, n || m || a) {
    int num[1005] = {0};
    num[a] = 1;
    struct line line[1005];
    rep(i, m) { cin >> line[i].h >> line[i].p >> line[i].q; }
    sort(line, m);
    rep(i, m) { swap(line[i].p, line[i].q, num); }
    rep(i, n + 1) {
      if (1 == num[i])
        cout << i << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

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

void swap(int a, int b, int num[105]) {
  int temp = num[a];
  num[a] = num[b];
  num[b] = temp;
}

void sort(struct line line[105], int m) {
  rep(i, m) {
    rep(j, m - 1) {
      if (line[j].h < line[j + 1].h) {
        struct line temp = line[j];
        line[j] = line[j + 1];
        line[j + 1] = temp;
      }
    }
  }
}

int main() {
  int n, m, a;
  while (cin >> n >> m >> a, n || m || a) {
    int num[1005];
    rep(i, n + 1) num[i] = i;
    struct line line[105];
    rep(i, m) { cin >> line[i].h >> line[i].p >> line[i].q; }
    sort(line, m);
    rep(i, m) { swap(line[i].p, line[i].q, num); }
    rep(i, m + 1) {
      if (a == num[i])
        cout << i << endl;
    }
  }
}
Problem p01126 (C++) — buggy submission, verdict: 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; i > 0; i--) {
      if (field[i][a] != 0) {
        a = field[i][a];
      }
    }
    printf("%d\\n", a);
  }
  return 0;
}

Error: 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 + 1; i > 0; i--) {
      if (field[i][a] != 0) {
        a = field[i][a];
      }
    }
    printf("%d\\n", a);
  }
  return 0;
}
Problem p01126 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>

using namespace std;

int main(void) {
  int n, m, a;
  int G[1001][101];

  while (true) {
    cin >> n >> m >> a;
    if (n + m + a == 0) {
      break;
    }
    memset(G, 0, sizeof(G));

    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      G[h][p] = q;
      G[h][q] = p;
    }
    for (int i = 1000; i >= 0; i--) {
      if (G[i][a] != 0) {
        a = G[i][a];
      }
    }
    cout << a << endl;
  }

  return 0;
}

Error: 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 + 1; i > 0; i--) {
      if (field[i][a] != 0) {
        a = field[i][a];
      }
    }
    printf("%d\\n", a);
  }
  return 0;
}
Problem p01126 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>

#define rep(i, j) for (int i = 0; i < j; i++)
#define all(i) i.begin(), i.end()

using namespace std;

int main() {
  int n = 0, m = 0, a = 0;
  while (cin >> n >> m >> a && n != 0) {
    vector<vector<int>> dat(1010, vector<int>(10000, 0));
    rep(i, m) {
      int tmp[3] = {0};
      cin >> tmp[0] >> tmp[1] >> tmp[2];
      dat[tmp[1]][tmp[0]] = tmp[2];
      dat[tmp[2]][tmp[0]] = tmp[1];
    }
    rep(i, 1001) {

      if (dat[a][1001 - i] == 0) {
        continue;
      } else if (dat[a][1001 - i] > 0) {
        a = dat[a][1001 - i];
      }
    }
    printf("%d\\n", a);
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>

#define rep(i, j) for (int i = 0; i < j; i++)
#define all(i) i.begin(), i.end()

using namespace std;

int main() {
  int n = 0, m = 0, a = 0;
  while (cin >> n >> m >> a && n != 0) {
    vector<vector<int>> dat(1010, vector<int>(10000, 0));
    rep(i, m) {
      int tmp[3] = {0};
      cin >> tmp[0] >> tmp[1] >> tmp[2];
      dat[tmp[1]][tmp[0]] = tmp[2];
      dat[tmp[2]][tmp[0]] = tmp[1];
    }
    rep(i, 1001) {
      if (dat[a][m - i] == 0) {
        continue;
      } else if (dat[a][m - i] > 0) {
        a = dat[a][m - i];
      }
    }
    printf("%d\\n", a);
  }

  return 0;
}
Problem p01126 (C++) — buggy submission, verdict: 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 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;
}

Error: 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 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;
}
Problem p01126 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define RFOR(i, m, n) for (int i = m; i >= n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

void search(int n, int m, int a) {
  vector<P> b[2000];

  rep(i, m) {
    int h, p, q;
    cin >> h >> p >> q;
    b[h].push_back(P(p, q));
  }

  rrep(i, 1000) {
    rep(j, b[i].size()) {
      if (a == b[i][j].first) {
        a = b[i][j].second;
        break;
      }
      if (a == b[i][j].second) {
        a = b[i][j].first;
        break;
      }
    }
  }

  cout << a << endl;
}

int main() {

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

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define RFOR(i, m, n) for (int i = m; i >= n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

void search(int n, int m, int a) {
  vector<P> b[200];

  rep(i, m) {
    int h, p, q;
    cin >> h >> p >> q;
    b[h].push_back(P(p, q));
  }

  rrep(i, n) {
    rep(j, b[i].size()) {
      if (a == b[i][j].first) {
        a = b[i][j].second;
        break;
      }
      if (a == b[i][j].second) {
        a = b[i][j].first;
        break;
      }
    }
  }

  cout << a << endl;
}

int main() {

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

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

#include <iostream>
using namespace std;

int main() {

  while (true) {
    int n = 0, m = 0, a = 0;
    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0) {
      break;
    }

    int amida[101][1001] = {{0}};

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

    for (int j = 1000; j > 0; j--) {
      if (amida[a][j] == 1)
        a++; //????????????
      else if (amida[a - 1][j] == 1)
        a--; //???
    }

    cout << a << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, m, a;
  int h, p, q;

  cin >> n >> m >> a;

  int amida[n + 1][1000];

  while (true) {
    cin >> h >> p >> q;
    if (h == 0 && p == 0 && q == 0) {
      break;
    }

    if (p < q)
      amida[p][h] = 1;
    else if (q < p)
      amida[q][h] = 1;
  }

  for (int j = 1000; j > 0; j--) {
    if (amida[a][j] == 1)
      a++; //????????????
    else if (amida[a - 1][j] == 1)
      a--; //???
  }

  cout << a;

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

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

int main() {
  while (true) {
    int n, m, a;
    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0) {
      return 0;
    }

    // 高さごとの横線のリスト
    vector<vector<pair<int, int>>> v(1000);
    for (int i = 0; i < m; ++i) {
      int h, p, q;
      cin >> h >> p >> q;
      v[h - 1].emplace_back(p, q);
    }
    reverse(begin(v), end(v));

    auto next = [&v](int h, int c) {
      auto it1 = find_if(begin(v[h]), end(v[h]),
                         [c](const auto &p) { return p.first == c; });
      if (it1 != end(v[h])) {
        return it1->second;
      }
      auto it2 = find_if(begin(v[h]), end(v[h]),
                         [c](const auto &p) { return p.second == c; });
      if (it2 != end(v[h])) {
        return it2->first;
      }
      return c;
    };

    int current = a;
    for (int i = 0; i < v.size(); ++i) {
      current = next(i, current);
    }
    cout << current << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

int main() {
  while (true) {
    int n, m, a;
    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0) {
      return 0;
    }

    // 高さごとの横線のリスト
    vector<vector<pair<int, int>>> v(m);
    for (int i = 0; i < m; ++i) {
      int h, p, q;
      cin >> h >> p >> q;
      v[m - h - 1].emplace_back(p, q);
    }

    auto next = [&v](int h, int c) {
      auto it1 = find_if(begin(v[h]), end(v[h]),
                         [c](const auto &p) { return p.first == c; });
      if (it1 != end(v[h])) {
        return it1->second;
      }
      auto it2 = find_if(begin(v[h]), end(v[h]),
                         [c](const auto &p) { return p.second == c; });
      if (it2 != end(v[h])) {
        return it2->first;
      }
      return c;
    };

    int current = a;
    for (int i = 0; i < m; ++i) {
      current = next(i, current);
    }
    cout << current << endl;
  }
}
Problem p01126 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  while (1) {
    int n, m, a;
    int h[1100][110] = {};
    cin >> n >> m >> a;
    if (n == m && m == a && a == 0) {
      break;
    }

    for (int i = 0; i < m; i++) {
      int x, y, z;
      cin >> x >> y >> z;
      h[x][y] = 1;
    }

    for (int i = 1000; i > 0; i--) {
      if (h[i][a] == 1) {
        a += 1;
      } else if (h[i][a - 1] == 1) {
        a -= 1;
      }
    }

    cout << a << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  while (1) {
    int n, m, a;
    int h[1100][110] = {};
    cin >> n >> m >> a;

    for (int i = 0;; i++) {
      int x, y, z;
      cin >> x >> y >> z;
      if (x == y && y == z && z == 0) {
        break;
      }
      h[x][y] = 1;
    }

    for (int i = 1100; i > 0; i--) {
      if (h[i][a] == 1) {
        a += 1;
      } else if (h[i][a - 1] == 1) {
        a -= 1;
      }
    }

    cout << a << endl;
  }

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

#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;
}

Error: TLE

Fixed submission:

#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;
}