結果

問題 No.2236 Lights Out On Simple Graph
ユーザー りあんりあん
提出日時 2023-03-03 21:36:37
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,754 bytes
コンパイル時間 4,951 ms
コンパイル使用メモリ 270,316 KB
実行使用メモリ 112,136 KB
最終ジャッジ日時 2023-10-18 01:32:43
合計ジャッジ時間 11,208 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 315 ms
15,972 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC target("avx2")
#pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;

using P = pair<int, int>;

const int M = 998244353;
const long long LM = 1LL << 60;

int n, m;
vector<vector<int>> edge;
vector<vector<int>> dist;

unordered_map<long long, long long> mp;

long long dfs(long long p) {
    if (p == 0) return 0;
    if (mp.count(p)) return mp[p];
    long long res = M;
    int fir = -1;
    for (int i = 0; i < 50; ++i) {
        if ((p >> i) & 1) {
            if (fir == -1) {
                fir = i;
            }
            else {
                res = min(res, dfs(p ^ (1LL << fir) ^ (1LL << i)) + dist[fir][i]);
            }
        }
    }
    return mp[p] = res;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cin >> n >> m;
    edge = vector<vector<int>>(n);
    for (int i = 0; i < m; ++i) {
        int u, v;
        cin >> u >> v;
        --u;
        --v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    vector<int> c(n);
    long long st = 0;
    for (int i = 0; i < n; ++i) {
        cin >> c[i];
        if (c[i] == 1) {
            st ^= 1LL << i;
        }
    }
    dist = vector<vector<int>>(n, vector<int>(n, M));
    for (int i = 0; i < n; ++i) {
        dist[i][i] = 0;
        for (auto& j : edge[i]) {
            dist[i][j] = 1;
        }
    }
    for (int k = 0; k < n; ++k) {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }

    long long res = dfs(st);
    if (res >= M) {
        cout << -1 << '\n';
    }
    else {
        cout << res << '\n';
    }



    return 0;
}
0