結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー kano_town
提出日時 2014-12-12 00:08:57
言語 Java
(openjdk 23)
結果
AC  
実行時間 200 ms / 5,000 ms
コード長 1,058 bytes
コンパイル時間 3,921 ms
コンパイル使用メモリ 78,056 KB
実行使用メモリ 57,276 KB
最終ジャッジ日時 2024-06-11 20:51:39
合計ジャッジ時間 10,733 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Yukicoder101 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[][] xy = new int[k + 1][2];
		for (int i = 0; i < k; i++) {
			xy[i + 1][0] = sc.nextInt();
			xy[i + 1][1] = sc.nextInt();
		}

		int[] loop = new int[n + 1];
		Arrays.fill(loop, 1);

		int pos, res = 1;
		for (int l = 1; l <= n; l++) {
			pos = l;
			for (int i = 1; i <= n; i++) {
				for (int j = 1; j <= k; j++) {
					if (xy[j][0] == pos) {
						pos = xy[j][1];
					} else if (xy[j][1] == pos) {
						pos = xy[j][0];
					}
				}
				if (l == pos) {
					loop[l] = i;
					break;
				}
			}
		}
		res = loop[1];
		for (int i = 2; i <= n; i++) {
			res = lcm(res, loop[i]);
		}
		System.out.println(res);

	}

	static int gcd(int n, int m) {
		int r = n % m;
		if (r == 0) return m;
		else return gcd(m, r);

	}

	static int lcm(int n, int m) {
		return n / gcd(n, m) * m;
	}

}
0