結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー mamekin
提出日時 2015-01-18 00:17:40
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,208 bytes
コンパイル時間 968 ms
コンパイル使用メモリ 95,516 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-22 18:16:45
合計ジャッジ時間 2,278 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

// 最大公約数
long long gcd(long long a, long long b){
    while(b != 0){
        long long tmp = a % b;
        a = b;
        b = tmp;
    }
    return a;
}

// 最小公倍数
long long lcm(long long a, long long b){
    return a / gcd(a, b) * b;
}

int main()
{
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for(int i=0; i<n; ++i)
        a[i] = i;

    while(--k >= 0){
        int x, y;
        cin >> x >> y;
        swap(a[x-1], a[y-1]);
    }

    vector<bool> check(n, false);
    long long ret = 1;
    for(int i=0; i<n; ++i){
        if(check[i])
            continue;

        int k = i;
        int len = 0;
        while(!check[k]){
            check[k] = true;
            k = a[k];
            ++ len;
        }
        ret = lcm(ret, len);
    }
    cout << ret << endl;

    return 0;
}
0