http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2001
#include <iostream> #include <vector> using namespace std; int main (int argc, char * const argv[]) { // insert code here... int n; while (cin >> n) { if (n == 0) { break; } //入力 int m,a; cin >> m; cin >> a; int h[m],p[m],q[m]; for (int i=0; i < m; i++) { cin >> h[i]; cin >> p[i]; cin >> q[i]; } //計算 for (int i=1000; i > 0; i--) { for (int j=0; j < m; j++) { if (h[j] == i) { if (a == p[j]) { a = q[j]; break; }else if (a == q[j]) { a = p[j]; break; } } } } cout << a << endl; } return 0; }