結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー 👑 rin204rin204
提出日時 2024-02-18 17:45:27
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,450 ms / 2,500 ms
コード長 2,296 bytes
コンパイル時間 1,298 ms
コンパイル使用メモリ 107,896 KB
実行使用メモリ 44,032 KB
最終ジャッジ日時 2024-02-18 17:45:49
合計ジャッジ時間 20,881 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 280 ms
13,184 KB
testcase_01 AC 839 ms
24,064 KB
testcase_02 AC 1,140 ms
40,320 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 363 ms
8,320 KB
testcase_07 AC 1,325 ms
43,392 KB
testcase_08 AC 1,255 ms
38,912 KB
testcase_09 AC 1,339 ms
44,032 KB
testcase_10 AC 1,428 ms
44,032 KB
testcase_11 AC 1,320 ms
44,032 KB
testcase_12 AC 1,316 ms
44,032 KB
testcase_13 AC 1,320 ms
44,032 KB
testcase_14 AC 1,450 ms
44,032 KB
testcase_15 AC 525 ms
6,548 KB
testcase_16 AC 557 ms
6,548 KB
testcase_17 AC 523 ms
6,548 KB
testcase_18 AC 563 ms
15,232 KB
testcase_19 AC 818 ms
34,560 KB
testcase_20 AC 733 ms
28,928 KB
testcase_21 AC 1,023 ms
44,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

class UnionFind {
private:
    int n;
    vector<int> par;
    int group_;
    
public:
    UnionFind(int n) : n(n), par(n, -1), group_(n) {}

    int find(int x) {
        if (par[x] < 0)
            return x;
        vector<int> lst;
        while (par[x] >= 0) {
            lst.push_back(x);
            x = par[x];
        }
        for (int y : lst) {
            par[y] = x;
        }
        return x;
    }

    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y)
            return false;
        if (par[x] > par[y])
            swap(x, y);
        par[x] += par[y];
        par[y] = x;
        group_--;
        return true;
    }

    int size(int x) {
        return -par[find(x)];
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    int group() {
        return group_;
    }
};

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> ab(m, vector<int>(2));
    for (int i = 0; i < m; ++i) {
        cin >> ab[i][0] >> ab[i][1];
    }
    vector<long long> C(n), W(10);
    for (int i = 0; i < n; ++i) {
        cin >> C[i];
        C[i]--;
    }
    for (int i = 0; i < 10; ++i) {
        cin >> W[i];
    }
    int t = 10;
    vector<UnionFind> UF(1 << t, UnionFind(n));
    vector<long long> cost(1 << t, 0);
    for (int bit = 0; bit < (1 << t); ++bit) {
        for (int i = 0; i < t; ++i) {
            if (bit >> i & 1) {
                cost[bit] += W[i];
            }
        }
    }
    for (auto& edge : ab) {
        edge[0]--;
        edge[1]--;
        int bit = (1 << C[edge[0]]) | (1 << C[edge[1]]);
        for (int S = 0; S < (1 << t); ++S) {
            if ((S & bit) == bit) {
                UF[S].unite(edge[0], edge[1]);
            }
        }
    }
    int Q;
    cin >> Q;
    for (int q = 0; q < Q; ++q) {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
    	long long ans = cost[(1 << t) - 1] + 1;
        for (int S = 0; S < (1 << t); ++S) {
            if (UF[S].same(x, y)) {
                ans = min(ans, cost[S]);
            }
        }
        if (ans == cost[(1 << t) - 1] + 1) {
            ans = -1;
        }
        cout << ans << endl;
    }
    return 0;
}
0