Problem p01126 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(t, i, c) for (t::iretator i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

const double EPS = 1e-10;

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

  while (cin >> n >> m >> a, n, m, a) {
    vvi amida(m, vi(3, 0));
    vi player(n, 0);

    REP(i, n) { player[i] = i + 1; }

    REP(i, m) {
      int h, p, q;
      cin >> h >> p >> q;

      amida[i][0] = h;
      amida[i][1] = p;
      amida[i][2] = q;
    }

    sort(RALL(amida));

    REP(i, m) {
      int p = amida[i][1];
      int q = amida[i][2];
      int player1 = player[p - 1];
      int player2 = player[q - 1];

      player[p - 1] = player2;
      player[q - 1] = player1;
    }

    REP(i, n) {
      if (player[i] == a) {
        cout << i + 1 << endl;
        break;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(t, i, c) for (t::iretator i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

const double EPS = 1e-10;

int main() {
  int i, maxh = 0;
  int n, m, a;
  int h, p, q;
  vvi data;

  int place;

  do {
    cin >> n >> m >> a;
    place = a;

    data.resize(m);
    for (i = 0; i < m; i++) {
      data[i].resize(3);
    }

    for (i = 0; i < m; i++) {
      cin >> h >> p >> q;
      data[i].at(0) = h;
      data[i].at(1) = p;
      data[i].at(2) = q;

      if (h > maxh) {
        maxh = h;
      }
    }

    while (maxh > 0) {
      for (i = 0; i < m; i++) {
        if (data[i].at(0) == maxh) {
          if (data[i].at(1) == place) {
            place = data[i].at(2);
          } else if (data[i].at(2) == place) {
            place = data[i].at(1);
          }
        }
      }
      --maxh;
    }

    cout << place << endl;

  } while (h != 0 && p != 0 && q != 0);
}