結果
| 問題 |
No.101 ぐるぐる!あみだくじ!
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2014-12-12 00:15:39 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 190 ms / 5,000 ms |
| コード長 | 1,144 bytes |
| コンパイル時間 | 3,583 ms |
| コンパイル使用メモリ | 77,652 KB |
| 実行使用メモリ | 43,744 KB |
| 最終ジャッジ日時 | 2024-06-11 20:52:00 |
| 合計ジャッジ時間 | 10,389 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
ソースコード
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);
}
}