結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー scachescache
提出日時 2014-12-12 00:15:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 207 ms / 5,000 ms
コード長 1,144 bytes
コンパイル時間 4,385 ms
コンパイル使用メモリ 78,496 KB
実行使用メモリ 58,936 KB
最終ジャッジ日時 2023-09-02 14:13:50
合計ジャッジ時間 13,325 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
55,780 KB
testcase_01 AC 199 ms
58,268 KB
testcase_02 AC 207 ms
58,264 KB
testcase_03 AC 144 ms
55,644 KB
testcase_04 AC 140 ms
55,672 KB
testcase_05 AC 207 ms
58,548 KB
testcase_06 AC 147 ms
55,776 KB
testcase_07 AC 195 ms
58,656 KB
testcase_08 AC 194 ms
56,688 KB
testcase_09 AC 199 ms
55,888 KB
testcase_10 AC 191 ms
55,600 KB
testcase_11 AC 193 ms
55,916 KB
testcase_12 AC 200 ms
58,548 KB
testcase_13 AC 192 ms
58,628 KB
testcase_14 AC 193 ms
55,896 KB
testcase_15 AC 182 ms
55,948 KB
testcase_16 AC 199 ms
58,296 KB
testcase_17 AC 201 ms
58,756 KB
testcase_18 AC 196 ms
55,948 KB
testcase_19 AC 193 ms
58,196 KB
testcase_20 AC 194 ms
56,676 KB
testcase_21 AC 194 ms
57,996 KB
testcase_22 AC 189 ms
56,252 KB
testcase_23 AC 203 ms
58,600 KB
testcase_24 AC 189 ms
56,096 KB
testcase_25 AC 199 ms
58,532 KB
testcase_26 AC 195 ms
58,140 KB
testcase_27 AC 196 ms
58,340 KB
testcase_28 AC 192 ms
57,016 KB
testcase_29 AC 195 ms
58,936 KB
testcase_30 AC 191 ms
57,960 KB
testcase_31 AC 200 ms
58,444 KB
testcase_32 AC 194 ms
58,248 KB
testcase_33 AC 198 ms
58,344 KB
testcase_34 AC 128 ms
55,792 KB
testcase_35 AC 128 ms
55,588 KB
testcase_36 AC 129 ms
55,432 KB
testcase_37 AC 145 ms
55,788 KB
testcase_38 AC 143 ms
55,400 KB
testcase_39 AC 143 ms
55,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #


import java.util.Scanner;

public class Main0100 {
	public static void main(String[] args) {
		Main0100 p = new Main0100();
	}

	public Main0100() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] x = new int[k];
		int[] y = new int[k];
		for (int i = 0; i < x.length; i++){
			x[i] = sc.nextInt()-1;
			y[i] = sc.nextInt()-1;
		}
		solve(n, x, y);
	}

	public void solve(int n, int[] x, int[] y) {
		int[] a = new int[n];
		int[] b = new int[n];
		for(int i=0;i<n;i++){
			a[i] = i;
			b[i] = i;
		}
		
		for(int i=0;i<x.length;i++){
			int w = a[x[i]];
			a[x[i]] = a[y[i]];
			a[y[i]] = w;
		}
		
		boolean[] isVisit = new boolean[n];
		int res = 1;
		for(int i=0;i<n;i++){
			if(isVisit[i])
				continue;
			int c = 1;
			int cur = i;
			while(a[cur] != i){
				isVisit[cur] = true;
				c++;
				cur = a[cur];
			}
			res = lcm(res, c);
		}
		
		System.out.println(res);
	}
	
	private int lcm(int a, int b){
		return a/gcd(a, b) * b;
	}

	private int gcd(int a, int b) {
		if(b==0)
			return a;
		return gcd(b, a%b);
	}
	

}
0