結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー ayataka5
提出日時 2024-02-16 23:29:16
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,714 bytes
コンパイル時間 1,921 ms
コンパイル使用メモリ 203,144 KB
最終ジャッジ日時 2025-02-19 14:58:39
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 5 WA * 17
権限があれば一括ダウンロードができます

ソースコード

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