結果

問題 No.901 K-ary εxtrεεmε
ユーザー 👑 emthrmemthrm
提出日時 2019-10-05 02:09:45
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 189 ms / 3,000 ms
コード長 5,218 bytes
コンパイル時間 2,094 ms
コンパイル使用メモリ 144,024 KB
実行使用メモリ 43,436 KB
最終ジャッジ日時 2024-04-15 00:26:08
合計ジャッジ時間 7,921 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
43,436 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 180 ms
37,560 KB
testcase_08 AC 175 ms
37,812 KB
testcase_09 AC 181 ms
37,808 KB
testcase_10 AC 181 ms
37,684 KB
testcase_11 AC 176 ms
37,556 KB
testcase_12 AC 176 ms
37,812 KB
testcase_13 AC 171 ms
37,556 KB
testcase_14 AC 171 ms
37,688 KB
testcase_15 AC 173 ms
37,692 KB
testcase_16 AC 176 ms
37,688 KB
testcase_17 AC 187 ms
37,812 KB
testcase_18 AC 189 ms
37,556 KB
testcase_19 AC 179 ms
37,684 KB
testcase_20 AC 175 ms
37,688 KB
testcase_21 AC 180 ms
37,684 KB
testcase_22 AC 174 ms
37,556 KB
testcase_23 AC 174 ms
37,688 KB
testcase_24 AC 168 ms
37,648 KB
testcase_25 AC 173 ms
37,816 KB
testcase_26 AC 179 ms
37,560 KB
testcase_27 AC 154 ms
37,612 KB
testcase_28 AC 161 ms
37,684 KB
testcase_29 AC 154 ms
37,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
using CostType = long long;
struct Edge {
  int src, dst; CostType cost;
  Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
  inline bool operator<(const Edge &rhs) const {
    return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src;
  }
  inline bool operator<=(const Edge &rhs) const { return cost <= rhs.cost; }
  inline bool operator>(const Edge &rhs) const {
    return cost != rhs.cost ? cost > rhs.cost : dst != rhs.dst ? dst > rhs.dst : src > rhs.src;
  }
  inline bool operator>=(const Edge &rhs) const { return cost >= rhs.cost; }
};

struct EulerTour {
  vector<int> tour, left, right, down, up, depth;
  vector<CostType> cost;

  EulerTour(const vector<vector<Edge> > &graph, int root = 0) : graph(graph) {
    int n = graph.size();
    left.resize(n);
    right.resize(n);
    down.assign(n, -1);
    up.assign(n, (n - 1) << 1);
    dfs(-1, root, 0);
  }

  void v_update(int ver, const function<void(int, int)> f) { f(left[ver], right[ver] + 1); }

  template <typename T>
  T v_query(int ver, const function<T(int, int)> f) { return f(left[ver], right[ver] + 1); }

  void e_update(int u, int v, const function<void(int, int)> f) { f(down[u] + 1, down[v] + 1); }

  template <typename T>
  T e_query(int u, int v, const function<T(int, int)> f) { return f(down[u] + 1, down[v] + 1); }

  void subtree_e_update(int ver, const function<void(int, int)> f) { f(down[ver] + 1, up[ver]); }

  template <typename T>
  T subtree_e_query(int ver, const function<T(int, int)> f) { return f(down[ver] + 1, up[ver]); }

private:
  const vector<vector<Edge> > graph;

  void dfs(int par, int ver, int now_depth) {
    left[ver] = tour.size();
    tour.emplace_back(ver);
    depth.emplace_back(now_depth);
    for (const Edge &e : graph[ver]) if (e.dst != par) {
      down[e.dst] = cost.size();
      cost.emplace_back(e.cost);
      dfs(ver, e.dst, now_depth + 1);
      tour.emplace_back(ver);
      depth.emplace_back(now_depth);
      up[e.dst] = cost.size();
      cost.emplace_back(-e.cost);
    }
    right[ver] = tour.size() - 1;
  }
};

struct LCA {
  vector<int> depth;
  vector<CostType> dist;

  LCA(const vector<vector<Edge> > &graph) : graph(graph) {
    n = graph.size();
    depth.resize(n);
    dist.resize(n);
    while ((1 << table_h) <= n) ++table_h;
    parent.resize(table_h, vector<int>(n));
  }

  void build(int root = 0) {
    dfs(-1, root, 0, 0);
    for (int i = 0; i + 1 < table_h; ++i) REP(ver, n) {
      parent[i + 1][ver] = (parent[i][ver] == -1 ? -1 : parent[i][parent[i][ver]]);
    }
  }

  int query(int u, int v) {
    if (depth[u] > depth[v]) swap(u, v);
    REP(i, table_h) {
      if ((depth[v] - depth[u]) >> i & 1) v = parent[i][v];
    }
    if (u == v) return u;
    for (int i = table_h - 1; i >= 0; --i) {
      if (parent[i][u] != parent[i][v]) {
        u = parent[i][u];
        v = parent[i][v];
      }
    }
    return parent[0][u];
  }

  CostType distance(int u, int v) { return dist[u] + dist[v] - dist[query(u, v)] * 2; }

private:
  int n, table_h = 1;
  vector<vector<Edge> > graph;
  vector<vector<int> > parent;

  void dfs(int par, int ver, int now_depth, CostType now_dist) {
    depth[ver] = now_depth;
    dist[ver] = now_dist;
    parent[0][ver] = par;
    for (const Edge &e : graph[ver]) {
      if (e.dst != par) dfs(ver, e.dst, now_depth + 1, now_dist + e.cost);
    }
  }
};

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n; cin >> n;
  vector<vector<Edge> > graph(n);
  REP(_, n - 1) {
    int u, v, w; cin >> u >> v >> w;
    graph[u].emplace_back(u, v, w);
    graph[v].emplace_back(v, u, w);
  }
  EulerTour et(graph);
  LCA lca(graph);
  lca.build();
  int q; cin >> q;
  while (q--) {
    int k; cin >> k;
    vector<pair<int, int> > x(k);
    REP(i, k) {
      cin >> x[i].second;
      x[i].first = et.left[x[i].second];
    }
    if (k == 1) {
      cout << 0 << '\n';
      continue;
    }
    sort(ALL(x));
    long long ans = 0;
    FOR(i, 1, k) ans += lca.distance(x[i - 1].second, x[i].second);
    ans += lca.distance(x[0].second, x[k - 1].second);
    cout << ans / 2 << '\n';
  }
  return 0;
}
0