yukicoder No.26 シャッフルゲーム

Problem

No.26 シャッフルゲーム - yukicoder

Solution

Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(reader.readLine());
        int M = Integer.parseInt(reader.readLine());

        String[] line;
        for (int i = 0; i < M; i++) {
            line = reader.readLine().split(" ");
            int p = Integer.parseInt(line[0]);
            int q = Integer.parseInt(line[1]);
            if (p == N) {
                N = q;
            } else if (q == N) {
                N = p;
            }
        }
        System.out.println(N);
    }
}