結果

問題 No.901 K-ary εxtrεεmε
ユーザー pekempeypekempey
提出日時 2019-10-04 22:38:01
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 288 ms / 3,000 ms
コード長 3,252 bytes
コンパイル時間 2,543 ms
コンパイル使用メモリ 190,244 KB
実行使用メモリ 27,224 KB
最終ジャッジ日時 2024-04-15 00:14:52
合計ジャッジ時間 8,993 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 145 ms
27,224 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 4 ms
6,940 KB
testcase_03 AC 5 ms
6,944 KB
testcase_04 AC 5 ms
6,944 KB
testcase_05 AC 5 ms
6,944 KB
testcase_06 AC 5 ms
6,944 KB
testcase_07 AC 205 ms
19,076 KB
testcase_08 AC 205 ms
18,940 KB
testcase_09 AC 197 ms
19,068 KB
testcase_10 AC 199 ms
19,196 KB
testcase_11 AC 200 ms
19,056 KB
testcase_12 AC 228 ms
19,028 KB
testcase_13 AC 229 ms
19,192 KB
testcase_14 AC 229 ms
19,204 KB
testcase_15 AC 226 ms
19,076 KB
testcase_16 AC 232 ms
19,200 KB
testcase_17 AC 175 ms
19,080 KB
testcase_18 AC 167 ms
19,072 KB
testcase_19 AC 175 ms
19,064 KB
testcase_20 AC 170 ms
19,204 KB
testcase_21 AC 174 ms
19,076 KB
testcase_22 AC 278 ms
19,844 KB
testcase_23 AC 275 ms
19,708 KB
testcase_24 AC 288 ms
19,844 KB
testcase_25 AC 279 ms
19,968 KB
testcase_26 AC 272 ms
19,844 KB
testcase_27 AC 159 ms
19,076 KB
testcase_28 AC 155 ms
19,200 KB
testcase_29 AC 164 ms
19,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rep2(i, l, r) for (int i = (l); i < (r); i++)
#define rep2r(i, l, r) for (int i = (r) - 1; i >= (l); i--)
#define range(a) a.begin(), a.end()

using namespace std;
using ll = long long;

struct HLD {
  vector<int> label, parent, head;
  vector<int> L, R;

  HLD(const vector<vector<int>> &g) : label(g.size()), parent(g.size()), head(g.size()), L(g.size()), R(g.size()) {
    const int n = g.size();
    vector<int> size(n, 1);
    auto dfs = [&](auto dfs, int u, int p) -> void {
      for (int v : g[u]) if (v != p) {
        dfs(dfs, v, u);
        size[u] += size[v];
      }
    };
    dfs(dfs, 0, -1);
    int k = 0;
    auto dfs2 = [&](auto dfs, int u, int p, int h) -> void {
      L[u] = k;
      label[u] = k++;
      head[u] = h;
      parent[u] = p;
      for (int v : g[u]) if (v != p && size[v] * 2 >  size[u]) dfs(dfs, v, u, h);
      for (int v : g[u]) if (v != p && size[v] * 2 <= size[u]) dfs(dfs, v, u, v);
      R[u] = k;
    };
    dfs2(dfs2, 0, -1, 0);
  }

  int lca(int u, int v) {
    for (;;) {
      if (label[u] > label[v]) swap(u, v);
      if (head[u] == head[v]) return u;
      v = parent[head[v]];
    }
  }

  template<class F> void each(int u, int v, F f) {
    for (;;) {
      if (label[u] > label[v]) swap(u, v);
      if (head[u] == head[v]) {
        f(label[u], label[v]);
        return;
      }
      f(label[head[v]], label[v]);
      v = parent[head[v]];
    }
  }

  template<class F> void each_edge(int u, int v, F f) {
    for (;;) {
      if (label[u] > label[v]) swap(u, v);
      if (head[u] == head[v]) {
        if (u != v) f(label[u] + 1, label[v]);
        return;
      }
      f(label[head[v]], label[v]);
      v = parent[head[v]];
    }
  }

  int operator[](int u) {
    return label[u];
  };
};


int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  int N; cin >> N;
  vector<vector<int>> G(N);
  vector<vector<pair<int, ll>>> GW(N);
  rep(i, N - 1) {
    int a, b, c; cin >> a >> b >> c;
    G[a].push_back(b);
    G[b].push_back(a);
    GW[a].emplace_back(b, c);
    GW[b].emplace_back(a, c);
  }
  HLD hld(G);
  vector<ll> dep(N);
  int k = 0;
  auto dfs = [&](auto dfs, int u, int p) -> void {
    for (auto e : GW[u]) if (e.first != p) {
      dep[e.first] = dep[u] + e.second;
      dfs(dfs, e.first, u);
    }
  };
  dfs(dfs, 0, -1);
  int Q; cin >> Q;
  while (Q--) {
    int k; cin >> k;
    vector<int> a(k); rep(i, k) cin >> a[i];
    priority_queue<pair<int, int>> q;
    ll ans = 0;
    map<int, int> par;
    rep(i, k) {
      q.emplace(hld[a[i]], a[i]);
      par[a[i]] = -1;
    }
    while (q.size() >= 2) {
      int u = q.top().second; q.pop();
      int v = q.top().second;
      int w = hld.lca(u, v);
      if (u != w && (par[u] == -1 || dep[par[u]] < dep[w])) par[u] = w;
      if (v != w && (par[v] == -1 || dep[par[v]] < dep[w])) par[v] = w;
      if (!par.count(w)) {
        par[w] = -1;
        q.emplace(hld[w], w);
      }
    }
    for (auto kv : par) {
      if (kv.second != -1) {
        ans += dep[kv.first] - dep[kv.second];
      }
    }
    cout << ans << '\n';
  }
}
0