結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー codershifth
提出日時 2015-08-04 23:48:40
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
RE  
実行時間 -
コード長 1,507 bytes
コンパイル時間 1,297 ms
コンパイル使用メモリ 161,892 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-18 01:21:29
合計ジャッジ時間 5,574 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 13 RE * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;


class GruGruAmidakuji {
public:
    void solve(void) {
            int N,K;
            cin>>N>>K;

            // F[i] := i を入力するとあみだくじをたどって F[i] に到達する
            vector<int> F(N);
            iota(RANGE(F),0);

            REP(i,K)
            {
                int x,y;
                cin>>x>>y;
                --x,--y;
                // 棒があるとそこで入力が交換される
                swap(F[x],F[y]);
            }
            // F^s(x) = F*F*...*F(x)=x (all x) なる最小の s を探せば良い
            vector<int> in(F);
            vector<int> out(N);
            FOR(s,1,100000)
            {
                bool ok = true;
                REP(i,N)
                {
                    if (in[i]!=i)
                        ok = false;
                    out[i] = F[in[i]];
                }
                if (ok)
                {
                    cout<<s<<endl;
                    return;
                }
                in = out;
            }
            assert(false);
            return;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new GruGruAmidakuji();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0