Problem p01126 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
#define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define erep(i, m, n) for (int(i) = (m); (i) <= (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrev(i, m, n) for (int(i) = (n)-1; (i) >= (m); (i)--)
#define erev(i, m, n) for (int(i) = (n); (i) >= (m); (i)--)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define vrep(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
template <class T, class S> inline pair<T, S> mp(T x, S y) {
  return make_pair(x, y);
}
template <class T, class S> inline bool minup(T &m, S x) {
  return m > (T)x ? (m = (T)x, true) : false;
}
template <class T, class S> inline bool maxup(T &m, S x) {
  return m < (T)x ? (m = (T)x, true) : false;
}

static const int INF = 1000000000;
static const ll MOD = 1000000007LL;
static const double EPS = 1E-12;

int n, m, a;
int h, p, q;

int main() {
  while (cin >> n >> m >> a, n) {
    vector<vi> vec(m, vi(3));
    rep(i, m) {
      cin >> h >> p >> q;
      vec[i][0] = h;
      vec[i][1] = p - 1;
      vec[i][2] = q - 1;
    }

    sort(ALL(vec), greater<vi>());

    vi shf(n);
    rep(i, n) shf[i] = i;
    rep(i, m) swap(shf[vec[i][1]], shf[vec[i][2]]);

    rep(i, n) if (a - 1 == shf[i]) cout << i + 1 << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
#define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define erep(i, m, n) for (int(i) = (m); (i) <= (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrev(i, m, n) for (int(i) = (n)-1; (i) >= (m); (i)--)
#define erev(i, m, n) for (int(i) = (n); (i) >= (m); (i)--)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define vrep(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
template <class T, class S> inline pair<T, S> mp(T x, S y) {
  return make_pair(x, y);
}
template <class T, class S> inline bool minup(T &m, S x) {
  return m > (T)x ? (m = (T)x, true) : false;
}
template <class T, class S> inline bool maxup(T &m, S x) {
  return m < (T)x ? (m = (T)x, true) : false;
}

static const int INF = 1000000000;
static const ll MOD = 1000000007LL;
static const double EPS = 1E-12;

int n, m, a;
int h, p, q;
vector<pair<int, pii>> in;

template <typename T> ostream &operator<<(ostream &r, const vector<T> t) {
  vrep(v, t) r << *v << (v + 1 == t.end() ? "" : " ");
  return r;
}
template <typename T>
ostream &operator<<(ostream &r, const pair<vector<T>, int> p) {
  int n = p.second;
  minup(n, p.first.size());
  rep(i, n - 1) r << p.first[i] << ' ';
  r << p.first[n - 1];
  return r;
}
template <typename T> ostream &operator<<(ostream &r, const pair<T *, int> p) {
  int n = p.second - 1;
  rep(i, n) r << p.first[i] << ' ';
  return r << p.first[n];
}
template <typename T, size_t S>
ostream &operator<<(ostream &r, const T (&a)[S]) {
  rep(i, S - 1) r << a[i] << ' ';
  return r << a[S - 1];
}

int main() {
  while (cin >> n >> m >> a, n) {
    rep(i, m) {
      cin >> h >> p >> q;
      in.pb(mp(h, mp(p - 1, q - 1)));
    }
    vi shf;
    rep(i, n) shf.pb(i);
    sort(ALL(in), greater<pair<int, pii>>());

    vrep(v, in) { swap(shf[v->second.first], shf[v->second.second]); }

    rep(i, shf.size()) if (shf[i] == a - 1) {
      cout << i + 1 << endl;
      break;
    }
  }

  return 0;
}