Language: C++

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

struct lines {
  int h;  //横線の高さ                                                                                                          
  int p, q; //横線に繋がる縦線の番号                                                                                            

  bool operator<(const lines &left) const {
    return h < left.h;
  }
};

int amida(int n, int m, int a, lines* line, int size) {
  for(int i = 0; i < size; i++) {
    if(line[i].p == a)
      a = line[i].q;
    else if(line[i].q == a)
      a = line[i].p;
  }
  return a;
}

int main() {
  int n; //縦線の本数                                                                                                           
  int m; //横線の本数                                                                                                           
  int a; //調べる縦線の番号                                                                                                     
  vector<lines> line;

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

    for(int i = 0; i < m; i++) {
      lines l;
      cin >> l.h >> l.p >> l.q;
      line.push_back(l);
    }

    sort(line.begin(), line.end(), greater<lines>());
    cout << amida(n, m, a, &line.front(), line.size()) << endl;
  }
  return 0;
}



Compiler output:
In file included from /usr/include/c++/14/string:49,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_function.h: In instantiation of 'constexpr bool std::greater<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = lines]':
/usr/include/c++/14/bits/predefined_ops.h:158:30:   required from 'constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = __gnu_cxx::__normal_iterator<lines*, std::vector<lines> >; _Iterator2 = __gnu_cxx::__normal_iterator<lines*, std::vector<lines> >; _Compare = std::greater<lines>]'
  158 |         { return bool(_M_comp(*__it1, *__it2)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1777:14:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<lines*, vector<lines> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<greater<lines> >]'
 1777 |           if (__comp(__i, __first))
      |               ~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1817:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<lines*, vector<lines> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<greater<lines> >]'
 1817 |           std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1908:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<lines*, vector<lines> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<greater<lines> >]'
 1908 |           std::__final_insertion_sort(__first, __last, __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4805:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<lines*, vector<lines> >; _Compare = greater<lines>]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:41:9:   required from here
   41 |     sort(line.begin(), line.end(), greater<lines>());
      |     ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_function.h:395:20: error: no match for 'operator>' (operand types are 'const lines' and 'const lines')
  395 |       { return __x > __y; }
      |                ~~~~^~~~~
In file included from /usr/include/c++/14/string:48:
/usr/include/c++/14/bits/stl_iterator.h:462:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator>(const reverse_iterator<_Iterator>&, const reverse_iterator<_Iterator>&)'
  462 |     operator>(const reverse_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:462:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_function.h:395:20: note:   'const lines' is not derived from 'const std::reverse_iterator<_Iterator>'
  395 |       { return __x > __y; }
      |                ~~~~^~~~~
/usr/include/c++/14/bits/stl_iterator.h:507:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator>(const reverse_iterator<_Iterator>&, const reverse_iterator<_IteratorR>&)'
  507 |     operator>(const reverse_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:507:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_function.h:395:20: note:   'const lines' is not derived from 'const std::reverse_iterator<_Iterator>'
  395 |       { return __x > __y; }
      |                ~~~~^~~~~
/usr/include/c++/14/bits/stl_iterator.h:1714:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator>(const move_iterator<_IteratorL>&, const move_iterator<_IteratorR>&)'
 1714 |     operator>(const move_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1714:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_function.h:395:20: note:   'const lines' is not derived from 'const std::move_iterator<_IteratorL>'
  395 |       { return __x > __y; }
      |                ~~~~^~~~~
/usr/include/c++/14/bits/stl_iterator.h:1774:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator>(const move_iterator<_IteratorL>&, const move_iterator<_IteratorL>&)'
 1774 |     operator>(const move_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1774:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_function.h:395:20: note:   'const lines' is not derived from 'const std::move_iterator<_IteratorL>'
  395 |       { return __x > __y; }
      |                ~~~~^~~~~