結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー uafr_csuafr_cs
提出日時 2015-09-11 18:14:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 195 ms / 5,000 ms
コード長 1,406 bytes
コンパイル時間 2,355 ms
コンパイル使用メモリ 77,524 KB
実行使用メモリ 59,272 KB
最終ジャッジ日時 2023-09-26 10:31:02
合計ジャッジ時間 11,639 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
53,852 KB
testcase_01 AC 184 ms
57,924 KB
testcase_02 AC 182 ms
58,584 KB
testcase_03 AC 142 ms
55,804 KB
testcase_04 AC 130 ms
55,928 KB
testcase_05 AC 195 ms
58,480 KB
testcase_06 AC 143 ms
55,416 KB
testcase_07 AC 191 ms
58,648 KB
testcase_08 AC 191 ms
56,316 KB
testcase_09 AC 187 ms
56,620 KB
testcase_10 AC 188 ms
55,896 KB
testcase_11 AC 185 ms
56,544 KB
testcase_12 AC 195 ms
58,200 KB
testcase_13 AC 194 ms
59,272 KB
testcase_14 AC 188 ms
55,836 KB
testcase_15 AC 188 ms
55,904 KB
testcase_16 AC 192 ms
58,352 KB
testcase_17 AC 193 ms
58,716 KB
testcase_18 AC 185 ms
56,092 KB
testcase_19 AC 187 ms
58,232 KB
testcase_20 AC 187 ms
55,684 KB
testcase_21 AC 192 ms
58,392 KB
testcase_22 AC 187 ms
55,896 KB
testcase_23 AC 192 ms
58,700 KB
testcase_24 AC 184 ms
56,136 KB
testcase_25 AC 191 ms
58,196 KB
testcase_26 AC 194 ms
58,648 KB
testcase_27 AC 193 ms
58,492 KB
testcase_28 AC 184 ms
55,796 KB
testcase_29 AC 194 ms
58,608 KB
testcase_30 AC 183 ms
58,244 KB
testcase_31 AC 192 ms
58,712 KB
testcase_32 AC 179 ms
58,244 KB
testcase_33 AC 195 ms
58,288 KB
testcase_34 AC 125 ms
55,824 KB
testcase_35 AC 125 ms
55,628 KB
testcase_36 AC 123 ms
55,568 KB
testcase_37 AC 139 ms
55,780 KB
testcase_38 AC 140 ms
56,104 KB
testcase_39 AC 139 ms
56,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class Main {

	public static void swap(int[] array, int a, int b){
		int tmp = array[a];
		array[a] = array[b];
		array[b] = tmp;
	}
	
	public static long gcd(long a, long b){
		return b == 0 ? a : gcd(b, a % b);
	}
	
	public static long lcm(long a, long b){
		return a / gcd(a, b) * b;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		final int K = sc.nextInt();
		
		int[] perms = new int[N];
		for(int i = 0; i < N; i++){
			perms[i] = i;
		}
		
		for(int i = 0; i < K; i++){
			final int x = sc.nextInt() - 1;
			final int y = sc.nextInt() - 1;
			
			swap(perms, x, y);
		}
		
		long answer = 1;
		boolean[] used = new boolean[N];
		while(true){
			int start_pos = -1;
			for(int i = 0; i < N; i++){
				if(!used[i]){
					start_pos = i;
					break;
				}
			}
			
			if(start_pos < 0){ break; }
			//System.out.println(start_pos + " " + Arrays.toString(perms));
			
			used[start_pos] = true;
			int current = start_pos;
			int count = 1;
			
			while(true){
				current = perms[current];
				if(current == start_pos){ break; }
				//System.out.println(current);

				used[current] = true;
				count++;
			}
			
			answer = lcm(answer, count);
		}
		
		System.out.println(answer);
		
		
	}

}
0