結果

問題 No.100 直列あみだくじ
ユーザー kimiyukikimiyuki
提出日時 2017-01-05 18:21:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4,802 ms / 5,000 ms
コード長 1,363 bytes
コンパイル時間 854 ms
コンパイル使用メモリ 76,880 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-15 17:43:19
合計ジャッジ時間 18,399 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <functional>
#include <sys/signal.h>
#include <sys/time.h>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
using namespace std;
void handler(int) {
    cout << "No" << endl;
    exit(EXIT_SUCCESS);
}
int main() {
    // timer
    signal(SIGALRM, handler);
    itimerval tv;
    tv.it_value.tv_sec = 4;
    tv.it_value.tv_usec = 800 * 1000;
    tv.it_interval = {};
    setitimer(ITIMER_REAL, &tv, NULL);
    // solve
    int n; cin >> n;
    vector<int> f(n); repeat (i,n) { cin >> f[i]; -- f[i]; }
    vector<int> g(n, -1), g_inv(n, -1);
    function<bool (int, int)> def = [&](int i, int j) {
        if (g    [i] != -1 and g    [i] != j) return false;
        if (g_inv[j] != -1 and g_inv[j] != i) return false;
        if (g[i] == j and g_inv[j] == i) return true;
        g[i] = j;
        g_inv[j] = i;
        return def(j, f[i]);
    };
    function<bool (int)> go = [&](int i) {
        while (i < n and g[i] != -1) ++ i;
        if (i == n) return true;
        repeat (j,n) if (g_inv[j] == -1) {
            vector<int> h = g;
            vector<int> h_inv = g_inv;
            if (def(i, j)) {
                if (go(i+1)) return true;
            }
            g = h;
            g_inv = h_inv;
        }
        return false;
    };
    cout << (go(0) ? "Yes" : "No") << endl;
    return 0;
}
0