結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー ayataka5ayataka5
提出日時 2024-02-16 23:29:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,714 bytes
コンパイル時間 2,490 ms
コンパイル使用メモリ 211,488 KB
実行使用メモリ 167,424 KB
最終ジャッジ日時 2024-02-16 23:29:35
合計ジャッジ時間 17,921 ms
ジャッジサーバーID
(参考情報)
judge16 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 469 ms
49,024 KB
testcase_19 AC 630 ms
126,208 KB
testcase_20 AC 578 ms
104,064 KB
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <template <class> class Container> class union_find {
protected:
  class node_type;

public:
  using container_type = Container<node_type>;
  using size_type = typename container_type::size_type;

protected:
  class node_type {
  public:
    typename union_find::size_type parent;
    typename union_find::size_type size;
  };

  container_type tree;

public:
  union_find() : tree() {}
  explicit union_find(const size_type size) : tree(size, {0, 1}) {
    for (size_type i = 0; i < size; ++i)
      tree[i].parent = i;
  }

  bool empty() const { return tree.empty(); }
  size_type size() const { return tree.size(); }

  size_type find(size_type x) {
    assert(x < size());
    while (tree[x].parent != x)
      x = tree[x].parent = tree[tree[x].parent].parent;
    return x;
  }
  bool same(const size_type x, const size_type y) {
    assert(x < size());
    assert(y < size());
    return find(x) == find(y);
  }
  size_type size(const size_type x) {
    assert(x < size());
    return tree[find(x)].size;
  }

  ::std::pair<size_type, size_type> unite(size_type x, size_type y) {
    assert(x < size());
    assert(y < size());
    x = find(x);
    y = find(y);
    if (x != y) {
      if (tree[x].size < tree[y].size)
        ::std::swap(x, y);
      tree[x].size += tree[y].size;
      tree[y].parent = x;
    }
    return {x, y};
  }
};

template<class T>
using vec_alias = ::std::vector<T>;

int main() {
    int N, M;
    cin >> N >> M;
    vector A(M, 0), B(M, 0);
    for(int i = 0; i < M; i++) { cin >> A[i] >> B[i]; A[i]--, B[i]--; }
    vector C(N, 0);
    for(int i = 0; i < N; i++) { cin >> C[i]; C[i]--; }
    vector W(10, 0LL);
    for(int i = 0; i < 10; i++) cin >> W[i];
    vector<union_find<vec_alias>> uf(1024, union_find<vec_alias>(N));
    for(int i = 0; i < M; i++) {
        for(int bit = 0; bit < 1024; bit++) {
            if((bit >> C[A[i]] & 1) && (bit >> C[B[i]] & 1)) {
                uf[bit].unite(A[i], B[i]);
                if(bit == 1) {
                    cout << A[i] << B[i] << endl;
                }
            }
        }
    }
    vector cost(1024, 0LL);
    for(int bit = 0; bit < 1024; bit++) {
        for(int i = 0; i < 10; i++) {
            if(bit >> i & 1) {
                cost[bit] += W[i];
            }
        }
    }
    int Q;
    cin >> Q;
    while(Q--) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        long long ans(20000000000LL);
        for(int bit = 0; bit < 1024; bit++) {
            if(uf[bit].same(u, v)) ans = min(cost[bit], ans);
        }
        if(ans == 20000000000LL) {
            ans = -1LL;
        }
        cout << ans << endl;
    }
    return 0;
}
0