Problem:
Amida, the City of Miracle
奇跡の都市アミダ（Amida, the City of Miracle）の市長は他の市のように選挙で選ぶのではない．かつて長い政権闘争と天変地異により疲弊したこの都市は，候補者全員を公平に選び，かつ幸運の星の下に生まれた者を市長にするために，候補者全員の運命をくじにゆだねることにしたのである．後にあみだくじと呼ばれるくじである．
選挙は以下のように行われる．候補者数と同数の長い縦線が引かれ，さらにいくつかの横線がそこに引かれる．横線は隣接した縦線の途中同士を結ぶように引かれる．いずれかの縦線の下には「当選」と書かれている．どの縦線が当選であるかは候補者からわからないように隠される．それぞれの候補者は縦線を 1 つ選択する．全ての候補者の選択が終わると，各候補者は線を上から下に向かってたどる．ただし，移動途中に横線が見つかったときは，横線の接続された反対側の縦線に移り，また下に向かってたどる．縦線の一番下までたどり着いたときに，そこに「当選」と書かれていた者が当選して，次期市長となる．
この方法はうまくいった．幸運な市長の下に，争いも災害も少ない平和な時代が続いた．しかし近年，人口増加のためにこの手法に限界が見えてきた．候補者が増えたため，あみだくじが大規模になり，手作業による集計では非常に時間がかかるようになったのである．そこで市では，市役所のプログラマーであるあなたにあみだくじの電算化を依頼した．
あなたの仕事は，あみだくじの構造と，選択された縦線の位置が与えられたときに，最終的にどの縦線にたどり着くかを求めるプログラムを書くことである．
以下の図は，サンプルとして与えた入力および出力の内容を示したものである．
Input
入力は複数のデータセットからなる．1 つのデータセットは以下のように与えられる．
n
m
a
横線1
横線2
横線3
...
横線
m
n
，
m
，
a
はそれぞれ 2 <=
n
<= 100, 0 <=
m
<= 1000, 1 <=
a
<=
n
をみたす整数であり，それぞれ縦線の本数，横線の本数，調べる縦線の番号を表す．
横線のデータは以下のように与えられる．
h
p
q
h
，
p
，
q
はそれぞれ 1 <=
h
<= 1000, 1 <=
p
<
q
<=
n
, を満たす整数であり，
h
はその横線の高さ，
p
，
q
はその横線につながっている 2 本の縦線の番号を表す．
1 つの縦線の同じ高さに，異なる横線が 2 つ以上つくことはない．
入力の終わりには，空白で区切られた 3 つのゼロのみからなる行がある。
Output
それぞれのデータセットに対して 1 行ずつ，縦線 a の上端からたどったときの下端における縦線の番号を出力しなさい．
Sample Input
4 4 1
3 1 2
2 2 3
3 3 4
1 3 4
0 0 0
Output for the Sample Input
4

Submitted solutions:

code_snippet: #include "bits/stdc++.h"

using namespace std;
#define FOR(i,s,t) for (int i = s; i < t; i++)
#define SZ(x) (int)x.size()
using LL = long long; using ll = LL;
using VI = vector<int>; using VL = vector<LL>;
const LL INF = 1e9; const LL LINF = 1e18;

typedef pair<ll, ll> pll;
ll solve(ll n,ll m,ll a) {
	ll res = -1;
	vector<pair<ll, pll>> x;
	vector<ll> amida(n + 1, 0);
	iota(amida.begin(), amida.end(), 0);
	for (int i = 0; i < m; i++) {
		ll h, p, q; cin >> h >> p >> q;
		x.push_back({ h,{p,q} });
	}
	sort(x.begin(), x.end());
	for (int i = 0; i < m; i++) {
		swap(amida[x[i].second.first], amida[x[i].second.second]);
	}
	res = amida[a];
	return res;
}

int main() {

	int n, m, a;
	while(cin >> n >> m >> a,n | m | a) {
		cout << solve(n, m, a) << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3192
final_rank: 3

code_snippet: // AOJ 2001 - Amida, the City of Miracle
// JAG Practice Contest for Japan Domestic 2006 - Problem B

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

bool solving() {
  int n, m, a; cin >> n >> m >> a;
  if(n == 0) return false;

  vector<pair<int, pair<int,int>>> line(m);
  for(int i=0; i<m; ++i) {
    cin >> line[i].first >> line[i].second.first >> line[i].second.second;
  }
  sort(line.begin(), line.end(), greater());

  for(int i=0; i<m; ++i) {
    if(a == line[i].second.first) {
      a = line[i].second.second;
    } else if(a == line[i].second.second) {
      a = line[i].second.first;
    }
  }
  cout << a << endl;
  return true;
}

int main() {
  while(solving()) {}
}
accuracy: 1
time_ms: 10
memory_kb: 3456
final_rank: 12

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

bool solve() {
    int N, M, id;
    cin >> N >> M >> id;
    if (N == 0 && M == 0 && id == 0) return false;
    id--;
    vector<tuple<int, int, int>> amida;
    while (M--) {
        int h, p, q;
        cin >> h >> p >> q;
        amida.emplace_back(h, p - 1, q - 1);
    }
    sort(amida.begin(), amida.end());
    vector<int> A(N);
    iota(A.begin(), A.end(), 1);
    for (auto [_, p, q] : amida) {
        swap(A[p], A[q]);
    }
    cout << A[id] << '\n';
    return true;
}

int main() {
    while (solve()) {}
}
accuracy: 1
time_ms: 10
memory_kb: 3456
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  while (true) {
    int n, m, a;
    cin >> n >> m >> a;
    if (n == 0) {
      break;
    }
    vector<int> h(m), p(m), q(m);

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

    vector<vector<int>> E(mh + 1, vector<int>(n + 1, -1));
    for (int i = 0; i < m; i++) {
      E[mh - h[i]][p[i]] = q[i];
      E[mh - h[i]][q[i]] = p[i];
    }

    int nw = a, nh = 0;
    while (true) {
      if (nh == mh) {
        break;
      }
      if (E[nh][nw] == -1) {
        nh++;
      } else {
        nw = E[nh][nw];
        nh++;
      }
    }
    cout << nw << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3652
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main() {
  while (1) {
    int n, m, a;
    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0) {
      break;
    }
    a--;
    vector<tuple<int, int, int>> A;
    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      p--;
      q--;
      A.emplace_back(h, p, q);
    }
    sort(A.begin(), A.end());
    vector<int> C(n);
    iota(C.begin(), C.end(), 0);
    for (int i = 0; i < m; i++) {
      auto [h, p, q] = A[i];
      swap(C[p], C[q]);
    }
    cout << C[a] + 1 << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3352
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){
    while(true){
        int n,m,a;
        cin >> n >> m >> a;
        if(n==0&&m==0&&a==0)break;
        vector<tuple<int,int,int>>G(m);

        for(int i=0;i<m;i++){
            int h_,p_,q_;
            cin >> h_ >> p_ >> q_;
            G[i]={h_,p_,q_};
        }

        sort(G.rbegin(),G.rend());
        int pos=a;
        int idx=1000;
        for(int i=0;i<G.size();i++){
            if(get<0>(G[i])==idx)continue;
            if(get<1>(G[i])==pos){
                pos=get<2>(G[i]);
                idx=get<0>(G[i]);
            }else if(get<2>(G[i])==pos){
                pos=get<1>(G[i]);
                idx=get<0>(G[i]);
            }
        }
        cout << pos << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3340
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(a) a.begin(),a.end()
#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)
#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)
const long long mod = 1000000007;
const int inf = 1e9;
const long long INF = 1e18;
ll myceil(ll a, ll b) {return (a+b-1)/b;}

int n,m,a;

void solve() {
  vector<vector<int>> v (m,vector<int> (3));
  rep(i,m) {
    cin >> v[i][0] >> v[i][1] >> v[i][2];
  }
  
  sort(all(v));
  reverse(all(v));
  
  rep(i,m) {
    if (a == v[i][1]) {
      a = v[i][2];
    }
    else if (a == v[i][2]) {
      a = v[i][1];
    }
  }
  cout << a << endl;
  return;
}

int main() {
  while (true) {
    cin >> n >> m >> a;
    if (n == 0) {
      return 0;
    }
    solve();
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3452
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
int ctoi(char c){return c-'0';}
ll ctoll(char c){return c-'0';}

void solve(int n, int m, int a){
     set<tuple<int,int,int>>amida;
     rep(i,m){
          int h,p,q;
          cin >> h >> p >> q;
          p--;q--;
          amida.insert(make_tuple(-h,p,q));
          amida.insert(make_tuple(-h,q,p));
     }

     a--;
     int now = a;
     int takasa = -1001;
     for(auto v : amida){
          int h,from,to;
          tie(h,from,to) = v;
          if(h > takasa && now == from){
               takasa = h;
               now = to;
          }
     }
     cout << now+1 << endl;
}

int main(){
     while(true){
          int n,m,a;
          cin >> n >> m >> a;
          if(n==0&&m==0&&a==0)break;

          solve(n,m,a);
     }
}
accuracy: 1
time_ms: 10
memory_kb: 3444
final_rank: 10

code_snippet: // (⁠◕⁠ᴗ⁠◕⁠✿⁠)

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define srep(i, s, n) for (ll i = s; i < (n); i++)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
template<typename T> using vvv = vv<vc<T>>;
using vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;
using ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;
using ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;
using uint = unsigned int;
using ull = unsigned long long;
const ld pi = 3.141592653589793;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
// const ll mod = 1000000007;
const ll mod = 998244353;
inline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }

#define debug(var)  do{std::cout << #var << " : \n";view(var);}while(0)
template<typename T> void view(T e){cout << e << endl;}
template<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << " "; } cout << endl;}
template<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }

int solve(int N, int M, int A){
    vc<tuple<int, int, int>> edge(M);;
    rep(i, M){
        int h, p, q; cin >> h >> p >> q;
        p--; q--;
        edge[i] = {h, p, q};
    }
    sort(all(edge), [&](auto i, auto j){return i > j;});
    vi P(N); rep(i, N) P[i] = i + 1;
    for (auto [h, p, q] : edge) swap(P[p], P[q]);
    rep(i, N) if (P[i] == A) return i + 1;
    return -1;
}

int main(){
    vc<int> ans;
    while (true){
        int N, M, A; cin >> N >> M >> A;
        if (N == 0) break;
        ans.push_back(solve(N, M, A));
    }
    for (auto x : ans) cout << x << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3152
final_rank: 2

code_snippet: #include <bits/stdc++.h>

using namespace std;

int main() {
    while(true) {
        int n, m, a;
        cin >> n >> m >> a;

        if (n == 0 && m == 0 && a == 0) {
            break;
        }

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

        int memo[1001][n+1];
        for (int i = 0; i < n+1; i++) {
            for (int j = 0; j < 1001; j++) {
                memo[j][i] = i;
            }
        }

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

        int pos = a;
        for (int i = 1000; i >= 1; i--) {
            pos = memo[i][pos];
        }

        cout << pos << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3688
final_rank: 18

code_snippet: #include <bits/stdc++.h>

using namespace std;

int main() {
    while(true) {
        int n, m, a;
        cin >> n >> m >> a;

        if (n == 0 && m == 0 && a == 0) {
            break;
        }

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

        int pos = a, high = 2000;
        while(true) {
            int nhigh = 0, idx = -1;
            for (int i = 0; i < m; i++) {
                if ((p[i] == pos || q[i] == pos) && nhigh < h[i] && h[i] < high) {
                    idx = i;
                    nhigh = h[i];
                }    
            }
            if (idx == -1) {
                break;
            }
            high = nhigh;
            if (p[idx] == pos) {
                pos = q[idx];
            } else {
                pos = p[idx];
            }
        }

        cout << pos << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3212
final_rank: 4

code_snippet: #include <iostream>
using namespace std;

class Amida {
public:
    int longNum, travNum, selectNum;
    int **array;

    Amida(int n, int m, int a) {
        longNum = n;
        travNum = m;
        selectNum = a;

        array = new int* [1001];
        for (int i = 0; i <= 1000; ++i)
            array[i] = new int [longNum + 1];
        
        for (int i = 0; i <= 1000; ++i)
            for (int j = 0; j <= longNum; ++j)
                array[i][j] = 0;
    }
    void set_traverse() {
        int h, p, q;

        for (int i = 1; i <= travNum; ++i) {
            cin >> h >> p >> q;

            array[h][p] = q;
            array[h][q] = p;
        }
    }
    int selection() {
        for (int i = 1000; i > 0; --i) {

            if (array[i][selectNum]) {
                selectNum = array[i][selectNum];
            }
        } 
        return selectNum;
    }
} ;

int main() {
    int n, m, a;
    cin >> n >> m >> a;
    while (n && m && a) {
        Amida ad(n, m, a);

        ad.set_traverse();

        cout << ad.selection() << '\n';

        cin >> n >> m >> a;
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 22072
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> Pi;
typedef pair<int, Pi> PPi;
int main(){
    int n, m, a, h, p, q;
    while(cin >> n >> m >> a, n){
        vector<PPi> vec;
        int num[101];
        for(int i=1;i<=n;i++) num[i] = i;
        for(int i=0;i<m;i++){
            cin >> h >> p >> q;
            vec.push_back(PPi(h, Pi(p, q)));
        }
        sort(vec.begin(), vec.end());
        for(int i=0;i<m;i++){
            Pi pi = vec[i].second;
            swap(num[pi.first], num[pi.second]);
        }
        cout << num[a] << endl;
    }
    return(0);
}
accuracy: 1
time_ms: 10
memory_kb: 2996
final_rank: 1

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <stdio.h>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <cassert>
#include <numeric>
#include <cstdio>


using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define P pair<int, int>


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

	cin >> n >> m >> a;
	while (n != 0) {
		vector<vector<int>>table(n, vector<int>(1001,-1));
		for (int i = 0;i < m;i++) {
			cin >> h >> p >> q;
			p--;q--;
			table[p][h] = q;
			table[q][h] = p;
		}
		for (int i = 0;i < n;i++) {
			reverse(table[i].begin(), table[i].end());
		}
		position = a - 1;
		for (int i = 0;i < 1001;i++) {
			if (table[position][i] != -1) {
				position = table[position][i];
			}
		}
		cout << position + 1 << endl;
		cin >> n >> m >> a;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3516
final_rank: 15

code_snippet: #include <iostream>
#include <vector>

using namespace std;

int main() {
    while (1) {
        int n, m, a;
        cin >> n >> m >> a;

        if (n == 0 && m == 0 && a == 0) {
            break;
        }

        vector<vector<pair<int, int>>> amida(1001);

        for (int i = 0; i < m; i++) {
            int h, p, q;
            cin >> h >> p >> q;
            pair<int, int> tmp(p, q);
            amida[h].push_back(tmp);
        }

        int curr = a;
        for (int i = 1000; i > 0; i--) {
            int length = amida[i].size();
            for (int j = 0; j < length; j++) {
                if (curr == amida[i][j].first) {
                    curr = amida[i][j].second;
                    break;
                } else if (curr == amida[i][j].second) {
                    curr = amida[i][j].first;
                    break;
                }
            }
        }

        cout << curr << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3380
final_rank: 9

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<set>

using namespace std;
using ll = long long;

int n,m,a;
void solve(){
    vector<pair<int,pair<int,int>>> g;
    for(int i = 0;i<m;i++){
        int u,v,h;
        cin>>h>>v>>u;
        u--;v--;
        g.push_back(make_pair(h,make_pair(u,v)));
    }
    sort(g.rbegin(),g.rend());
    a--;
    for(int i = 0;i<m;i++){
        if(g[i].second.first==a) a = g[i].second.second;
        else if(g[i].second.second==a) a= g[i].second.first;
    }
    cout<<a+1<<endl;
        
}

int main(){
    while(true){
        cin>>n>>m>>a;
        if(n==0) break;
        solve();
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3468
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main () {
    int n, m, a;
    cin >> n >> m >> a;
    
    while (n != 0) {
        int line = 1;
        vector<vector<int>> x(2000, vector<int>(n));
        for (int i = 0; i < m; i++) {
            int h, p, q;
            cin >> h >> p >> q;
            x[h][p - 1] = line;
            x[h][q - 1] = line;
            line++;
        }
        
        int now = a - 1;
        for (int i = 1999; i >= 0; i--) {
            if (x[i][now] != 0) {
                if (x[i][now - 1] == x[i][now]) now--;
                else if (x[i][now + 1] == x[i][now]) now++;
            }
        }
        
        cout << now + 1 << endl;
        
        cin >> n >> m >> a;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3900
final_rank: 20

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

#define rep(i, N) for(int i = 0; i < N; i++)
int main(){
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	while(1){
		int N, M, A; cin >> N >> M >> A;
		if(N == 0) break;
		if(M == 0){
			cout << A << endl;
			continue;
		}
		vector<int> h(M), p(M), q(M);
		rep(i, M){
			cin >> h[i] >> p[i] >> q[i]; 
		}

		vector<vector<int>> G(1010, vector<int>(110, -1));
		rep(i, M){
			G[h[i]][p[i]] = q[i];
			G[h[i]][q[i]] = p[i];
		}
		for(int i = 1005; i >= 0; i--){
			if(G[i][A] != -1){
				A = G[i][A];
			}
		}
		cout << A << '\n';
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3548
final_rank: 16

code_snippet: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i< (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int mod = 998244353;
const int inf = (1<<30);
 
int main(){
	while(1){
		int n,m,a;
		cin>>n>>m>>a;
		if(n == 0) break;
		vector<int> x(n);
		rep(i,n) x[i] = i;
		vector<pair<int,P>> bou(m);

		rep(i,m){
			int h,p,q;
			cin>>h>>p>>q;
			p--;q--;
			bou[i] = {h,{p,q}};
		}
		sort(bou.begin(),bou.end());

		rep(i,m){
			P p = bou[i].second;
			swap(x[p.first] , x[p.second]);
		}
		cout<<x[a-1]+1<<endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3352
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
using tup = tuple<int, int, int>;

int solve(int N, int M, int A) {
    vector<tup> P(M); 
    for (int i = 0 ; i < M ; i++) {
        auto& [h, p, q] = P[i];
        cin >> h >> p >> q;
    }
    sort(P.begin(), P.end());
    reverse(P.begin(), P.end());
    int ans = A;
    for (auto [h, p, q] : P) {
        // cerr << h << ' ' << p << ' ' << q << endl;
        if (ans == p) {
            // cerr << "ans hit" << p << " " << "move to" << q << endl;
            ans = q;
        }
        else if (ans == q) {
            // cerr << "ans hit" << q << " " << "move to" << p << endl;
            ans = p;
        }
    }
    return ans;
}


int main() {
    while (1) {
        int N, M, A; cin >> N >> M >> A;
        if (!N) break;
        int ans = solve(N, M, A);
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3312
final_rank: 5