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

#include <bits/stdc++.h>
using namespace std;
#define M 1001
int main() {
  int n, m, a, p, q, h, max = 0, b[M][3] = {};
  while (1) {
    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0)
      break;
    for (int i = 0; i < m; i++) {
      cin >> h >> p >> q;
      b[i][0] = h;
      b[i][1] = p;
      b[i][2] = q;
      if (max < h)
        max = h;
    }

    for (int j = max; j >= 0; j--)
      for (int i = 0; i < m; i++)
        if (b[i][0] == j)
          if (b[i][1] == a)
            a = b[i][2];
          else if (b[i][2] == a)
            a = b[i][1];

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, a, h, max = 0, b[1001][5][5] = {};
  cin >> n >> m >> a;

  while (1) {
    cin >> h >> n >> m;
    if (h == 0 && n == 0 && m == 0)
      break;
    b[h][n][m] = 1;
    if (max < h)
      max = h;
  }

  for (int i = max; i >= 0; i--) {
    if (b[i][a][a + 1] == 1)
      a += 1;
    else if (b[i][a - 1][a] == 1)
      a -= 1;
  }
  cout << a << endl;

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

Error: TLE

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

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

int main() {
  int n, m, a, mh;
  mh = 0;
  while (1) {
    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0)
      break;
    vector<vector<int>> h;
    h.resize(m);
    for (int i = 0; i < m; i++) {
      h[i].resize(3);
    }
    for (int i = 0; i < m; i++) {
      cin >> h[i][0] >> h[i][1] >> h[i][2];
      if (h[i][0] > mh)
        mh = h[i][0];
    }
    for (int i = mh; i > 0; i--) {
      for (int j = 0; j < m; j++) {
        if (h[j][0] == i) {
          if (h[j][1] == a)
            a = h[j][2];
          else if (h[j][2] == a)
            a = h[j][1];
        }
      }
    }
    cout << a << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int n, m, a, mh;
  mh = 0;
  cin >> n >> m >> a;
  vector<vector<int>> h;
  h.resize(m);
  for (int i = 0; i < m; i++) {
    h[i].resize(3);
  }
  for (int i = 0; i < m; i++) {
    cin >> h[i][0] >> h[i][1] >> h[i][2];
    if (h[i][0] > mh)
      mh = h[i][0];
  }
  for (int i = mh; i > 0; i--) {
    for (int j = 0; j < m; m++) {
      if (h[j][0] == i) {
        if (h[j][1] == a)
          a = h[j][2];
        else if (h[j][2] == a)
          a = h[j][1];
      }
    }
  }
  cout << a << endl;
  return 0;
}
Problem p01126 (C++) — buggy submission, verdict: 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[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;
}

Error: TLE

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

#include <iostream>

using namespace std;

#define MMAX 1000
#define NMAX 1000

int main() {

  int n, m, a;
  int h, p, q;
  int amida[MMAX + 2][NMAX + 2];

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

    for (int i = 0; i <= MMAX + 1; i++) {
      for (int j = 0; j <= NMAX + 1; j++) {
        amida[i][j] = 0;
      }
    }

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

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

    cout << a << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

#define MAX 100

#define MMAX 100
#define NMAX 100

int main() {

  int n, m, a;
  int h, p, q;
  int amida[MMAX + 2][NMAX + 2];

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

    for (int i = 0; i <= m + 1; i++) {
      for (int j = 0; j <= n + 1; j++) {
        amida[i][j] = 0;
      }
    }

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

    for (int i = n - 1; i >= 1; i--) {
      if (amida[i][a] == 1)
        a += 1;
      else if (amida[i][a - 1] == 1)
        a -= 1;
    }

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

// #! g++ -O3 amida_the_city_of_miracle.cpp

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

struct line {
  int number;
  int left;
  int right;
};

class lesser {
public:
  bool operator()(const struct line first, const struct line second) {
    return first.number > second.number;
  }
};

int main() {

  while (1) {
    int n, m, a;

    // input
    cin >> n >> m >> a;
    if (!n && !m && !a)
      break;
    list<struct line> lines;
    struct line tmp;
    for (int i = 0; i < m; i++) {
      cin >> tmp.number >> tmp.left >> tmp.right;
      lines.push_back(tmp);
    }

    // initialization
    int columns[n];
    for (int i = 0; i < n; i++) {
      columns[i] = i + 1;
    }
    lines.sort(lesser());

    // swap
    list<struct line>::iterator it;
    for (it = lines.begin(); it != lines.end(); it++) {
      int tmp = columns[(*it).left - 1];
      columns[(*it).left - 1] = columns[(*it).right - 1];
      columns[(*it).right - 1] = tmp;
    }

    // output
    for (int i = 0; i < n; i++) {
      if (columns[i] == a) {
        cout << i + 1 << endl;
      }
    }
  }
  return 0;
}

Error: WA

Fixed submission:

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

struct line {
  int number;
  int left;
  int right;
};

class lesser {
public:
  bool operator()(const struct line first, const struct line second) {
    return first.number > second.number;
  }
};

int main() {

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

    // input
    cin >> n >> m >> a;
    if (!n && !m && !a)
      break;
    list<struct line> lines;
    struct line tmp;
    for (int i = 0; i < m; i++) {
      cin >> tmp.number >> tmp.left >> tmp.right;
      lines.push_back(tmp);
    }

    // initialization
    int columns[m];
    for (int i = 0; i < m; i++) {
      columns[i] = i + 1;
    }
    lines.sort(lesser());

    // swap
    list<struct line>::iterator it;
    for (it = lines.begin(); it != lines.end(); it++) {
      int tmp = columns[(*it).left - 1];
      columns[(*it).left - 1] = columns[(*it).right - 1];
      columns[(*it).right - 1] = tmp;
    }

    // output
    for (int i = 0; i < m; i++) {
      if (columns[i] == a) {
        cout << i + 1;
      }
    }
  }
  return 0;
}
Problem p01126 (C++) — buggy submission, verdict: 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(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;
}

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

#include <algorithm>
#include <iostream>

using namespace std;

#define M 1002
#define N 102

int main(int argc, char const *argv[]) {
  int n, m, a;
  int h, p, q;

  int f[M][N];

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

    for (int i1 = 0; i1 < M; i1++) {
      for (int i2 = 0; i2 < N; i2++) {
        f[i1][i2] = 0;
      }
    }

    for (int i1 = 0; i1 < m; i1++) {
      cin >> h >> p >> q;
      f[M - h][p] = 1;
    }

    for (int i1 = 0; i1 < M; i1++) {
      if (f[i1][a - 1] == 1)
        a--;
      else if (f[i1][a] == 1)
        a++;
    }
    cout << a << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

#define M 10002
#define N 102

int main(int argc, char const *argv[]) {
  int n, m, a;
  int h, p, q;

  int f[M][N];

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

    for (int i1 = 0; i1 < M; i1++) {
      for (int i2 = 0; i2 < N; i2++) {
        f[i1][i2] = 0;
      }
    }

    for (int i1 = 0; i1 < m; i1++) {
      cin >> h >> p >> q;
      f[m - h][p] = 1;
    }

    for (int i1 = 0; i1 < m; i1++) {
      if (f[i1][a - 1] == 1)
        a--;
      else if (f[i1][a] == 1)
        a++;
    }
    cout << a << endl;
  }
  return 0;
}
Problem p01126 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

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

  while (true) {
    scanf("%d %d %d", &n, &m, &a);
    if (n == 0) {
      break;
    }

    int nico[128];
    for (int i = 0; i < n; i++) {
      nico[i] = i + 1;
    }

    int h, p, q;
    vector<pair<int, pair<int, int>>> amida;

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

      amida.push_back(make_pair(h, make_pair(p, q)));
    }

    sort(amida.begin(), amida.end());

    for (int i = 0; i < m; i++) {
      swap(nico[amida[i].second.first - 1], nico[amida[i].second.second - 1]);
    }

    printf("%d\\n", nico[a - 1]);
  }

  return (0);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, m, a;
  int amida[1024][1024];

  while (true) {
    scanf("%d %d %d", &n, &m, &a);
    if (n == 0) {
      break;
    }

    for (int i = 0; i < 1024; i++) {
      for (int j = 0; j < 1024; j++) {
        amida[i][j] = 0;
      }
    }

    int h, p, q;

    for (int i = 0; i < n; i++) {
      scanf("%d %d %d", &h, &p, &q);

      amida[h][p] = q;
    }

    int x = a;
    for (int y = n; y > 0; y--) {
      if (amida[y][x] != 0) {
        x = amida[y][x];
      }
    }

    printf("%d\\n", x);
  }

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

#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) cerr << #x << "=" << static_cast<bitset<16>>(x) << endl;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))

const int INF = 10000000;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
using namespace std;
typedef pair<int, int> P;

/** Problem2001 : Amida, the City of Miracle **/
int main() {
  int amida[1010][110];
  int n, m, a, h, p, q;
  while (cin >> n >> m >> a, n || m || a) {
    fill(amida[0], amida[0] + 1010 * 110, 0);
    rep(i, m) {
      cin >> h >> p >> q;
      amida[1001 - h][p] = 1;
      amida[1001 - h][q] = -1;
    }

    rep(i, 1001) { a += amida[i][a]; }
    cout << a << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) cerr << #x << "=" << static_cast<bitset<16>>(x) << endl;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))

const int INF = 10000000;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
using namespace std;
typedef pair<int, int> P;

/** Problem2001 : Amida, the City of Miracle **/
int main() {
  int amida[110][1010];
  int n, m, a, h, p, q;
  while (cin >> n >> m >> a, n || m || a) {
    fill(amida[0], amida[0] + 1010 * 110, 0);
    rep(i, n) {
      cin >> h >> p >> q;
      amida[h][p] = 1;
      amida[h][q] = -1;
    }

    for (int i = n; i >= 0; i--) {
      a += amida[i][a];
    }
    cout << a << endl;
  }
}
Problem p01126 (C++) — buggy submission, verdict: 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) {
      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;
  }
}

Error: 0

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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
#define bv vector<bool>
using namespace std;
typedef pair<int, int> PI;
#define MAX 99999999
int main() {
  int n, m, a;
  while (cin >> n >> m >> a, n + m + a) {
    vvi v(1001, vi(1001, INF));
    PI now;
    int h, p, q;
    rep(i, m) {
      cin >> h >> p >> q;
      v[h][p] = q;
      v[h][q] = p;
    }
    now.first = 1000;
    now.second = a;
    while (true) {
      if (now.first == 0)
        break;
      if (v[now.first][now.second] != INF) {
        now.second = v[now.first][now.second];
      }
      now.first--;
    }
    cout << now.second << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
#define bv vector<bool>
using namespace std;
typedef pair<int, int> PI;
#define MAX 99999999
int main() {
  int n, m, a;
  while (cin >> n >> m >> a, n + m + a) {
    vvi v(n + 1, vi(m + 1, INF));
    PI now;
    int h, p, q;
    rep(i, m) {
      cin >> h >> p >> q;
      v[h][p] = q;
    }
    now.first = --n;
    now.second = a;
    // cout<<now.first<<endl;
    while (true) {
      if (now.first == 0)
        break;
      if (v[now.first][now.second] != INF) {
        now.second = v[now.first][now.second];
        // cout<<now.second<<endl;
        now.first--;
      } else
        now.first--;
    }
    cout << now.second << endl;
  }
}
Problem p01126 (C++) — buggy submission, verdict: 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[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;
}

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

Error: TLE

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