結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー outlineoutline
提出日時 2021-02-14 18:26:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 3,329 bytes
コンパイル時間 1,754 ms
コンパイル使用メモリ 148,328 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-29 10:21:16
合計ジャッジ時間 5,988 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

struct StronglyConnectedComponents{
    using G = vector<vector<int>>;

    G graph;
    G rev;
    int n;
    int scc_sz = 0;
    G scc_graph;

    vector<int> scc_id;
    vector<int> vs;
    vector<bool> visited;

    StronglyConnectedComponents() = default;

    StronglyConnectedComponents(int V) : n(V) {
        graph.resize(n);
        rev.resize(n);
        scc_id.assign(n, -1);
        visited.assign(n, false);
    }

    StronglyConnectedComponents(G& g) : graph(g), n(g.size()) {
        rev.resize(n);
        scc_id.assign(n, -1);
        visited.assign(n, false);
        
        for(int u = 0; u < n; ++u){
            for(int v : graph[u])   rev[v].push_back(u);
        }
    }

    void resize(int V){
        n = V;
        graph.resize(n);
        rev.resize(n);
        scc_id.assign(n, -1);
        visited.assign(n, false);
    }

    void add_edge(int u, int v){
        graph[u].emplace_back(v);
        rev[v].emplace_back(u);
    }

    void dfs(int from){
        visited[from] = true;
        for(int to : graph[from]){
            if(!visited[to])  dfs(to);
        }
        vs.push_back(from);
    }

    void rdfs(int from, int k){
        scc_id[from] = k;
        for(int to : rev[from]){
            if(scc_id[to] == -1)    rdfs(to, k);
        }
    }

    void build(){
        for(int v = 0; v < n; ++v){
            if(!visited[v]) dfs(v);
        }
        reverse(vs.begin(), vs.end());

        for(int v : vs){
            if(scc_id[v] == -1)    rdfs(v, scc_sz++);
        }

        scc_graph.resize(scc_sz);
        for(int v = 0; v < n; ++v){
            scc_graph[scc_id[v]].push_back(v);
        }

        return;
    }

    int size() { return scc_sz; }

    vector<int> get_set(int k) { return scc_graph[k]; }

    int operator[](int v) { return scc_id[v]; }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, K;
    cin >> N >> K;
    vector<int> amida(N);
    for(int i = 0; i < N; ++i)  amida[i] = i;
    for(int i = 0; i < K; ++i){
        int X, Y;
        cin >> X >> Y;
        swap(amida[X - 1], amida[Y - 1]);
    }
    StronglyConnectedComponents g(N);
    for(int i = 0; i < N; ++i){
        g.add_edge(amida[i], i);
    }
    g.build();
    int sz = g.size();
    ll LCM = 1;
    for(int i = 0; i < sz; ++i){
        int x = g.get_set(i).size();
        LCM = LCM / gcd(LCM, x) * x;
    }
    cout << LCM << endl;

    return 0;
}
0