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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

using namespace std;

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

  while (cin >> n >> m >> a) {
    if (!n)
      break;
    vector<vector<pair<int, int>>> line(1000);
    REP(i, m) {
      cin >> h >> p >> q;
      line[h - 1].push_back(make_pair(p, q));
    }

    RREP(i, line.size()) {
      REP(j, line[i].size()) {
        if (line[i][j].first == a)
          a = line[i][j].second;
        else if (line[i][j].second == a)
          a = line[i][j].first;
      }
    }
    cout << a << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

using namespace std;

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

  while (cin >> n >> m >> a) {
    if (!n)
      break;
    vector<vector<pair<int, int>>> line(m);
    REP(i, m) {
      cin >> h >> p >> q;
      line[h - 1].push_back(make_pair(p, q));
    }

    RREP(i, m - 1) {
      REP(j, line[i].size()) {
        if (line[i][j].first == a)
          a = line[i][j].second;
        else if (line[i][j].second == a)
          a = line[i][j].first;
      }
    }
    cout << a << endl;
  }

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

Error: 0

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 - 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;
  }
}
Problem p01126 (C++) — buggy submission, verdict: 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;
vector<P> v[1000];
int main() {
  int n, m, a;
  while (cin >> n >> m >> a, n) {
    a--;
    rep(i, 1000) v[i].clear();
    int h, p, q;
    rep(i, m) {
      cin >> h >> p >> q;
      v[h - 1].push_back(P(p - 1, q - 1));
    }
    rep(k, 1000) {
      int i = 999 - k;
      rep(j, v[i].size()) {
        if (v[i][j].first == a)
          a = v[i][j].second;
        else if (v[i][j].second == a)
          a = v[i][j].first;
      }
    }
    cout << a + 1 << endl;
  }
  return 0;
}


Error: TLE

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;
vector<P> v[1000];
int main() {
  int n, m, a;
  while (1) {
    cin >> n >> m >> a;
    a--;
    if (n == 0 && m == 0 && a == 0)
      break;
    rep(i, 1000) v[i].clear();
    int h, p, q;
    rep(i, m) {
      cin >> h >> p >> q;
      v[h - 1].push_back(P(p - 1, q - 1));
    }
    rep(k, 1000) {
      int i = 999 - k;
      rep(j, v[i].size()) {
        if (v[i][j].first == a)
          a = v[i][j].second;
        else if (v[i][j].second == a)
          a = v[i][j].first;
      }
    }
    cout << a + 1 << endl;
  }
  return 0;
}
Problem p01126 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstring>
#include <iostream>
using namespace std;
int x[1180][118][118], n, m, p, a, b, c;
int main() {
  while (true) {
    memset(x, 0, sizeof(x));
    cin >> n >> m >> p;
    if (!n) {
      break;
    }
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> c;
      x[a][b][c] = 1;
      x[a][c][b] = 1;
    }
    for (int i = 1000; i > 0; i--) {
      for (int j = 1; j <= n; j++) {
        if (x[i][p][j] == 1) {
          p = j;
          break;
        }
      }
    }
    cout << p << endl;
  }
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
int x[1200][120][120], n, m, p, a, b, c;
int main() {
  while (true) {
    memset(x, 0, sizeof(x));
    cin >> n >> m >> p;
    if (!n) {
      break;
    }
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> c;
      x[a][b][c] = 1;
      x[a][c][b] = 1;
    }
    for (int i = 1000; i >= 0; i--) {
      for (int j = 1; j <= n; j++) {
        if (x[i][p][j] == 1) {
          p = j;
        }
      }
    }
    cout << p << endl;
  }
}
Problem p01126 (C++) — buggy submission, verdict: 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(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;
}


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

#include <algorithm>
#include <functional>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

typedef pair<int, int> bou;
int main() {
  int n, m, a;
  while (cin >> n >> m >> a && n | m | a != 0) {

    vector<pair<int, bou>> yokobou;
    int high = 0;
    int h, p, q;
    // while(cin>>h>>p>>q&&h|p|q!=0){
    for (int i = 0; i < m; i++) {
      cin >> h >> p >> q;
      if (high < h)
        high = h;
      yokobou.push_back(make_pair(h, bou(p, q)));
    }
    // sort
    sort(yokobou.begin(), yokobou.end(), greater<pair<int, bou>>());

    /* for_each(yokobou.begin(), yokobou.end(), [](pair <int, pair <int, int> >
       elm){ cout << elm.first << " " << elm.second.first << " " <<
       elm.second.second << endl;
       });*/

    // 今いる縦棒 今いる高さ
    int now = a;
    for (int h = 1000; h > 0; h--) {
      for (int i = 0; i < m; i++) {
        if (h != yokobou[i].first)
          continue;
        ;
        if (yokobou[i].second.first == now) {
          now = yokobou[i].second.second;
          /*        yokobou.erase(yokobou.begin()+i);
                    m--;*/
        } else if (yokobou[i].second.second == now) {
          now = yokobou[i].second.first;
          // yokobou.erase(yokobou.begin()+i);
          // m--;
        }
      }
    }
    cout << now << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

typedef pair<int, int> bou;
int main() {
  int n, m, a;
  while (cin >> n >> m >> a && n | m | a != 0) {

    vector<pair<int, bou>> yokobou;
    int high = 0;
    int h, p, q;
    // while(cin>>h>>p>>q&&h|p|q!=0){
    for (int i = 0; i < m; i++) {
      cin >> h >> p >> q;
      if (high < h)
        high = h;
      yokobou.push_back(make_pair(h, bou(p, q)));
    }
    // sort
    sort(yokobou.begin(), yokobou.end(), greater<pair<int, bou>>());

    /* for_each(yokobou.begin(), yokobou.end(), [](pair <int, pair <int, int> >
       elm){ cout << elm.first << " " << elm.second.first << " " <<
       elm.second.second << endl;
       });*/

    // 今いる縦棒 今いる高さ
    pair<int, int> aPerson = make_pair(a, high);
    while (aPerson.second != 0) {
      for (int i = 0; i < m; i++) {
        if (yokobou[i].first == aPerson.second &&
            yokobou[i].second.first == aPerson.first) {
          aPerson = make_pair(yokobou[i].second.second, yokobou[i].first - 1);
          /*        yokobou.erase(yokobou.begin()+i);
                    m--;*/
        } else if (yokobou[i].first == aPerson.second &&
                   yokobou[i].second.second == aPerson.first) {
          aPerson = make_pair(yokobou[i].second.first, yokobou[i].first - 1);
          // yokobou.erase(yokobou.begin()+i);
          // m--;
        }
      }
    }
    cout << aPerson.first << endl;
  }
  return 0;
}
Problem p01126 (C++) — buggy submission, verdict: 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 0;
}

Error: 1

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

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

using namespace std;

struct bar {
  int h;
  int p;
  int q;
};
vector<bar> vec;

void bable_sort() {
  for (int i = 0; i < vec.size() - 1; i++) {
    for (int j = vec.size() - 1; j > i; j--) {
      if (vec[j - 1].h < vec[j].h) {
        swap(vec[j], vec[j - 1]);
      }
    }
  }
}

int main() {
  int n, m, a;
  bar b;
  while (cin >> n >> m >> a) {
    if (n == 0 && m == 0 && a == 0)
      break;
    vec.clear();
    for (int i = 0; i < m; i++) {
      cin >> b.h >> b.p >> b.q;
      vec.push_back(b);
    }
    bable_sort();
    for (int i = 0; i < vec.size(); i++) {
      if (vec[i].p == a) {
        a = vec[i].q;
      } else if (vec[i].q == a) {
        a = vec[i].p;
      }
    }
    cout << a << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

struct bar {
  int h;
  int p;
  int q;
};
vector<bar> vec;

void bable_sort() {
  for (int i = 0; i < vec.size() - 1; i++) {
    for (int j = i; j < vec.size() - 1; j++) {
      if (vec[j].h < vec[j + 1].h) {
        swap(vec[j], vec[j + 1]);
      }
    }
  }
}

int main() {
  int n, m, a;
  bar b;
  while (cin >> n >> m >> a, n + m + a) {
    for (int i = 0; i < m; i++) {
      cin >> b.h >> b.p >> b.q;
      vec.push_back(b);
    }
    bable_sort();

    for (int i = 0; i < vec.size(); i++) {
      if (vec[i].p == a) {
        a = vec[i].q;
      } else if (vec[i].q == a) {
        a = vec[i].p;
      }
    }
    cout << a << endl;
  }
  return 0;
}
Problem p01126 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

using namespace std;

int main(void) {
  int n, m, a;
  char map[1001][101];
  while (cin >> n >> m >> a, n | m | a) {
    for (int y = 1; y < 1001; ++y)
      for (int x = 1; x <= n; ++x)
        map[y][x] = 0;
    int Maxh = 0;
    for (int i = 0; i < m; ++i) {
      int h, p, q;
      cin >> h >> p >> q;
      map[h][p] = q;
      map[h][q] = p;
      Maxh = max(Maxh, h);
    }
    for (int i = Maxh; i > 0; --i) {
      if (map[i][a]) {
        a = (int)map[i][a];
      }
    }
    cout << a << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int main(void) {
  int n, m, a;
  char map[101][1001];
  while (cin >> n >> m >> a, n | m | a) {
    for (int y = 1; y < m; ++y)
      for (int x = 1; x < n; ++x)
        map[y][x] = 0;
    int Maxh = 0;
    for (int i = 0; i < m; ++i) {
      int h, p, q;
      cin >> h >> p >> q;
      map[h][p] = q;
      map[h][q] = p;
      Maxh = max(Maxh, h);
    }
    for (int i = Maxh; i > 0; --i) {
      if (map[i][a])
        a = map[i][a];
    }
    cout << a << endl;
  }
  return 0;
}
Problem p01126 (C++) — buggy submission, verdict: 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; h++)
      for (i = 1; i <= n; i++)
        memo[h][i] = 0;
  }
  return 0;
}


Error: TLE

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; i++)
      for (i = 1; i <= n; i++)
        memo[h][i] = 0;
  }
  return 0;
}
Problem p01126 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int yoko[1001][1001];

int main() {
  int n, m, a, h, p, q;
  while (cin >> n >> m >> a) {
    if (n == 0 && m == 0 && a == 0)
      break;
    memset(yoko, 0, sizeof(yoko));
    for (int i = 0; i < m; i++) {
      cin >> h >> p >> q;
      yoko[h][p] = 1;
    }
    for (int i = 1000; i >= 1; i--) {
      if (yoko[i][a] == 1)
        a++;
      else if (yoko[i][a - 1] == 1)
        a--;
    }
    cout << a << endl;
  }
}

Error: TLE

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 yoko[1001][1001];

int main() {
  int n, m, a;
  cin >> n >> m >> a;
  int h, p, q;
  while (true) {
    cin >> h >> p >> q;
    if (h == 0 && p == 0 && q == 0)
      break;
    yoko[h][p] = 1;
  }
  for (int i = m; i <= 1; i--) {
    for (int j = 1; j < n - 1; j++) {
      if (yoko[m][n] == 1) {
        n++;
        continue;
      } else if (yoko[m][n - 1]) {
        n--;
        continue;
      }
    }
  }
  cout << n << endl;
}
Problem p01126 (C++) — buggy submission, verdict: 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 = 999; i >= 0; i--) {
      a = kuji[i][a];
    }
    cout << a + 1 << endl;
  }
  return 0;
}

Error: -11

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 = 1000; i >= 0; i--) {
      a = kuji[i][a];
    }
    cout << a + 1 << endl;
  }
  return 0;
}
Problem p01126 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

#define MAX 1024
typedef pair<int, int> C;

int main(void) {
  while (1) {
    set<C> comp[MAX];
    int n, m, a;
    int h, p, q;
    int now, highest = -1;

    scanf("%d %d %d", &n, &m, &a);
    if (!n && !m && !a)
      break;
    now = a;

    for (int i = 0; i < m; i++) {
      scanf("%d %d %d", &h, &p, &q);
      comp[h - 1].insert(C(p, q));
      if (h - 1 > highest)
        highest = h - 1;
    }

    for (int i = highest; i >= 0; i--) {
      for (int j = -1; j <= 1; j += 2) {
        if (comp[i].find(C(now + j, now)) != comp[i].end() ||
            comp[i].find(C(now, now + j)) != comp[i].end()) {
          now += j;
          break;
        }
      }
    }
    printf("%d\\n", now);
  }
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

#define MAX 1024
typedef pair<int, int> C;

int main(void) {
  while (1) {
    set<C> comp[MAX];
    int n, m, a;
    int h, p, q;
    int now, highest = -1;

    scanf("%d %d %d", &n, &m, &a);
    if (!n && !m && !a)
      break;
    now = a;

    for (int i = 0; i < n; i++) {
      scanf("%d %d %d", &h, &p, &q);
      comp[h - 1].insert(C(p, q));
      if (h - 1 > highest)
        highest = h;
    }

    for (int i = highest; i >= 0; i--) {
      for (int j = -1; j <= 1; j += 2) {
        if (comp[i].find(C(now + j, now)) != comp[i].end() ||
            comp[i].find(C(now, now + j)) != comp[i].end()) {
          now += j;
          break;
        }
      }
    }
    printf("%d\\n", now);
  }
  return 0;
}
Problem p01126 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

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

    vector<priority_queue<pair<int, int>>> queue(n);
    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      queue[p - 1].push(make_pair(h, q));
      queue[q - 1].push(make_pair(h, p));
    }

    int now, now_h;
    now = a;
    now_h = 1001;
    while (!queue[now - 1].empty()) {
      int tmp, tmp_h;
      while (1) {
        tmp = queue[now - 1].top().second;
        tmp_h = queue[now - 1].top().first;
        queue[now - 1].pop();
        if (tmp_h < now_h) {
          break;
        }
        if (queue[now - 1].empty()) {
          tmp = now;
          tmp_h = now_h;
          break;
        }
      }
      now = tmp;
      now_h = tmp_h;
    }
    cout << now << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main(void) {
  int n, m, a;
  cin >> n >> m >> a;
  vector<priority_queue<pair<int, int>, vector<pair<int, int>>,
                        greater<pair<int, int>>>>
      queue(n);
  while (1) {
    int h, p, q;
    cin >> h >> p >> q;
    if (h == 0 and p == 0 and q == 0) {
      break;
    }
    queue[p - 1].push(make_pair(h, q));
    queue[q - 1].push(make_pair(h, p));
  }

  int now, now_h;
  now = a;
  now_h = 1001;
  while (!queue[now - 1].empty()) {
    int tmp, tmp_h;
    while (1) {
      tmp = queue[now - 1].top().second;
      tmp_h = queue[now - 1].top().first;
      queue[now - 1].pop();
      if (tmp_h < now_h) {
        break;
      }
      if (queue[now - 1].empty()) {
        tmp = now;
        tmp_h = now_h;
        break;
      }
    }
    now = tmp;
    now_h = tmp_h;
  }

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

#include <iostream>
#include <string.h>
using namespace std;
int main() {
  while (1) {
    int n, m, a;
    int lottery[1001][101];
    memset(lottery, 0, sizeof(lottery));
    cin >> n >> m >> a;
    if (n == 0)
      break;
    while (m-- > 0) {
      int h, p, q;
      cin >> h >> p >> q;
      lottery[1000 - h][p] = q;
      lottery[1000 - h][q] = p;
    }
    for (int i = 1; i <= 1000; i++) {
      for (int j = 1; j <= 100; j++) {
        if (lottery[i][j] && a == j) {
          a = lottery[i][j];
          break;
        }
      }
    }
    cout << a << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
using namespace std;
typedef pair<int, int> P;
int main() {
  while (1) {
    int n, m, a;
    cin >> n >> m >> a;
    if (n == 0)
      break;
    multimap<int, P> cross;
    while (m-- > 0) {
      int h, p, q;
      cin >> h >> p >> q;
      cross.insert(pair<int, P>(h, P(p, q)));
    }
    multimap<int, P>::iterator it = cross.end();
    it--;
    for (; it != cross.end(); it--) {
      if ((*it).second.first == a) {
        a = (*it).second.second;
      }
    }
    if ((*it).second.first == a) {
      a = (*it).second.second;
    }
    cout << a << endl;
  }
}
Problem p01126 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> Q;
typedef pair<int, Q> P;
const int INF = 1 << 29;

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

  while (cin >> n >> m >> a, n | m | a) {
    vector<P> v;
    int h, p, q;
    for (int i = 0; i < m; i++) {
      cin >> h >> p >> q;
      v.push_back(P(h, Q(p, q)));
    }
    sort(v.begin(), v.end(), greater<P>());

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> Q;
typedef pair<int, Q> P;
const int INF = 1 << 29;

int t;

int solve(vector<P> v, int a, int b) {
  for (int i = b; i < (int)v.size(); i++) {
    if (v[i].first == t)
      return v[b].second.second;
    if (v[i].second.first == a) {
      solve(v, v[i].second.second, i);
    }
  }

  return -1;
}

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

  while (cin >> n >> m >> a, n | m | a) {
    vector<P> v;
    int h, p, q;
    t = INF;
    for (int i = 0; i < m; i++) {
      cin >> h >> p >> q;
      t = min(t, h);
      v.push_back(P(h, Q(p, q)));
    }
    sort(v.begin(), v.end(), greater<P>());
    cout << solve(v, a, 0) << endl;
  }

  return 0;
}