結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー 37zigen37zigen
提出日時 2020-03-30 18:40:17
言語 Java19
(openjdk 21)
結果
AC  
実行時間 215 ms / 5,000 ms
コード長 1,184 bytes
コンパイル時間 4,258 ms
コンパイル使用メモリ 81,196 KB
実行使用メモリ 58,872 KB
最終ジャッジ日時 2023-09-04 16:09:42
合計ジャッジ時間 12,959 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,712 KB
testcase_01 AC 194 ms
58,816 KB
testcase_02 AC 193 ms
58,316 KB
testcase_03 AC 152 ms
55,924 KB
testcase_04 AC 137 ms
55,708 KB
testcase_05 AC 196 ms
58,872 KB
testcase_06 AC 144 ms
55,648 KB
testcase_07 AC 193 ms
58,152 KB
testcase_08 AC 192 ms
56,920 KB
testcase_09 AC 186 ms
56,312 KB
testcase_10 AC 192 ms
56,004 KB
testcase_11 AC 191 ms
56,096 KB
testcase_12 AC 215 ms
58,600 KB
testcase_13 AC 213 ms
58,680 KB
testcase_14 AC 191 ms
56,180 KB
testcase_15 AC 199 ms
58,248 KB
testcase_16 AC 197 ms
58,324 KB
testcase_17 AC 190 ms
58,540 KB
testcase_18 AC 190 ms
55,896 KB
testcase_19 AC 190 ms
58,000 KB
testcase_20 AC 184 ms
56,544 KB
testcase_21 AC 192 ms
58,732 KB
testcase_22 AC 186 ms
55,868 KB
testcase_23 AC 183 ms
58,212 KB
testcase_24 AC 182 ms
56,732 KB
testcase_25 AC 193 ms
58,316 KB
testcase_26 AC 193 ms
58,164 KB
testcase_27 AC 196 ms
58,208 KB
testcase_28 AC 192 ms
56,340 KB
testcase_29 AC 198 ms
58,188 KB
testcase_30 AC 191 ms
58,028 KB
testcase_31 AC 190 ms
58,284 KB
testcase_32 AC 191 ms
58,384 KB
testcase_33 AC 196 ms
58,200 KB
testcase_34 AC 125 ms
55,812 KB
testcase_35 AC 125 ms
56,180 KB
testcase_36 AC 126 ms
55,896 KB
testcase_37 AC 144 ms
56,000 KB
testcase_38 AC 143 ms
53,488 KB
testcase_39 AC 140 ms
55,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	long gcd(long a,long b) {
		return a==0?b:gcd(b%a,a);
	}
	
	long lcm(long a,long b) {
		return a/gcd(a,b)*b;
	}
	
	int dfs(int cur,int par,ArrayList<Integer>[] g,boolean[] vis) {
		if(vis[cur])return 0;
		int sz=1;
		vis[cur]=true;
		for(int dst:g[cur]) {
			if(dst==par)continue;
			sz+=dfs(dst,cur,g,vis);
		}
		return sz;
	}
	
	void run() {
		Scanner sc=new Scanner(System.in);
		int N=sc.nextInt();
		int K=sc.nextInt();
		ArrayList<Integer>[] g=new ArrayList[N];
		for(int i=0;i<N;++i)g[i]=new ArrayList<Integer>();
		int[] ord=new int[N];
		for(int i=0;i<N;++i)ord[i]=i;
		for(int i=0;i<K;++i) {
			int x=sc.nextInt();
			int y=sc.nextInt();
			--x;--y;
			ord[x]^=ord[y];ord[y]^=ord[x];ord[x]^=ord[y];
		}
		for(int i=0;i<N;++i) {
			g[i].add(ord[i]);
		}
		long ans=1;
		boolean[] vis=new boolean[N];
		for(int i=0;i<N;++i) {
			if(vis[i])continue;
			ans=lcm(ans,dfs(i,-1,g,vis));
		}
		System.out.println(ans);
	}
	
	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}

0