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

#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
void swap(vector<int> &x, int i, int j) {
  int tmp;
  tmp = x[i];
  x[i] = x[j];
  x[j] = tmp;
}
void qSort(vector<int> &x, vector<int> &p, vector<int> &q, int left,
           int right) {
  //  cout << "aa" << endl;
  int i, j;
  int pivot;
  i = left;
  j = right;

  pivot = x[(j + i) / 2];
  while (true) {
    while (x[i] < pivot)
      i++;
    while (pivot < x[j])
      j--;
    if (i >= j)
      break;
    swap(x, i, j);
    swap(p, i, j);
    swap(q, i, j);
    i++;
    j--;
  }
  if (left < i - 1) {
    qSort(x, p, q, left, i - 1);
  }
  if (j + 1 < right + 1) {
    qSort(x, p, q, j + 1, right);
  }
}
int check(vector<int> h) {
  for (int i = 0; i < h.size() - 1; i++) {
    if (h[i] <= h[i + 1]) {

    } else
      return 0;
  }
  return 1;
}
int main() {
  vector<int> h;
  vector<int> p;
  vector<int> q;
  int m, n, a;
  int current; // = a;
  int flag;    // = -1;
  int ff = 0;
  while (true) {

    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0) {
      return 0;
    }

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

    if (h.size() > 1)
      qSort(h, p, q, 0, h.size() - 1);
    /* for(int i = 0;i < h.size();i++){
      cout<< h[i] << " " << p[i]<< " " << q[i]<<endl;
    }
    return 0;*/
    /*if(check(h)){
      cout << "yes" << endl;
    }else{
      cout << "no" << endl;
      }*/

    current = a;
    flag = -1;
    for (int i = h.size() - 1; i >= 0; i--) {
      if (flag != h[i]) {
        if (q[i] == current) {
          flag = h[i];
          current = p[i];
        } else if (p[i] == current) {
          flag = h[i];
          current = q[i];
        }
      }
    }

    cout << current << endl;
    /*ff++;
    cout << ff << endl;
    std::vector<int>().swap(h);
    std::vector<int>().swap(q);
    std::vector<int>().swap(p);*/
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
void swap(vector<int> &x, int i, int j) {
  int tmp;
  tmp = x[i];
  x[i] = x[j];
  x[j] = tmp;
}
void qSort(vector<int> &x, vector<int> &p, vector<int> &q, int left,
           int right) {
  int i, j;
  int pivot;
  i = left;
  j = right;

  pivot = x[(left + right) / 2];
  while (true) {
    while (x[i] < pivot)
      i++;
    while (pivot < x[j])
      j--;
    if (i >= j)
      break;
    swap(x, i, j);
    swap(p, i, j);
    swap(q, i, j);
    i++;
    j--;
  }
  if (left < i - 1) {
    qSort(x, p, q, left, i - 1);
  }
  if (j + 1 < right) {
    qSort(x, p, q, j + 1, right);
  }
}

int main() {
  while (true) {
    int m, n, a;
    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0) {
      return 0;
    }
    vector<int> h = vector<int>(n);
    vector<int> p = vector<int>(n);
    vector<int> q = vector<int>(n);
    for (int i = 0; i < n; i++) {
      cin >> h[i] >> p[i] >> q[i];
    }

    if (h.size() > 1)
      qSort(h, p, q, 0, h.size() - 1);
    /* for(int i = 0;i < h.size();i++){
      cout<< h[i] << " " << p[i]<< " " << q[i]<<endl;
      }
      return 0;*/
    int current = a;
    int flag = -1;
    for (int i = h.size() - 1; i >= 0; i--) {
      if (flag != h[i]) {
        if (q[i] == current) {
          flag = h[i];
          current = p[i];
        } else if (p[i] == current) {
          flag = h[i];
          current = q[i];
        }
      }
    }

    cout << current << endl;
  }
}