結果

問題 No.1718 Random Squirrel
ユーザー 👑 emthrmemthrm
提出日時 2021-10-22 22:53:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 10,268 bytes
コンパイル時間 3,281 ms
コンパイル使用メモリ 239,324 KB
実行使用メモリ 72,868 KB
最終ジャッジ日時 2024-09-23 06:41:06
合計ジャッジ時間 9,542 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
5,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 2 ms
5,376 KB
testcase_09 WA -
testcase_10 AC 2 ms
5,376 KB
testcase_11 WA -
testcase_12 RE -
testcase_13 WA -
testcase_14 WA -
testcase_15 RE -
testcase_16 RE -
testcase_17 WA -
testcase_18 RE -
testcase_19 RE -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 RE -
testcase_24 WA -
testcase_25 RE -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 112 ms
63,164 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 132 ms
72,860 KB
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
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()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int DY[]{1, 0, -1, 0}, DX[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}, DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

struct EulerTour {
  std::vector<int> tour, left, right, down, up, depth;

  EulerTour(const std::vector<std::vector<int>> &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);
    int idx = 0;
    dfs(-1, root, 0, idx);
  }

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

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

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

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

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

private:
  const std::vector<std::vector<int>> graph;

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

template <typename Band>
struct SparseTable {
  using Fn = std::function<Band(Band, Band)>;

  SparseTable() {}

  SparseTable(const std::vector<Band> &a, Fn fn) { init(a, fn); }

  void init(const std::vector<Band> &a, Fn fn_) {
    is_built = true;
    fn = fn_;
    int n = a.size(), table_h = 0;
    lg.assign(n + 1, 0);
    for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
    while ((1 << table_h) <= n) ++table_h;
    dat.assign(table_h, std::vector<Band>(n));
    for (int j = 0; j < n; ++j) dat[0][j] = a[j];
    for (int i = 1; i < table_h; ++i) for (int j = 0; j + (1 << i) <= n; ++j) {
      dat[i][j] = fn(dat[i - 1][j], dat[i - 1][j + (1 << (i - 1))]);
    }
  }

  Band query(int left, int right) const {
    assert(is_built && left < right);
    int h = lg[right - left];
    return fn(dat[h][left], dat[h][right - (1 << h)]);
  }

private:
  bool is_built = false;
  Fn fn;
  std::vector<int> lg;
  std::vector<std::vector<Band>> dat;
};

struct LowestCommonAncestor : EulerTour {
  LowestCommonAncestor(const std::vector<std::vector<int>> &graph, int root = 0)
  : EulerTour(graph, root) {
    int n = this->tour.size();
    std::vector<P> nodes(n);
    for (int i = 0; i < n; ++i) nodes[i] = {this->depth[i], this->tour[i]};
    st.init(nodes, [](P a, P b) -> P { return std::min(a, b); });
  }

  int query(int u, int v) const {
    u = this->left[u];
    v = this->left[v];
    if (u > v) std::swap(u, v);
    return st.query(u, v + 1).second;
  }

private:
  using P = std::pair<int, int>;

  SparseTable<P> st;
};

struct HeavyLightDecomposition {
  std::vector<int> parent, subtree, id, inv, head;

  HeavyLightDecomposition(const std::vector<std::vector<int>> &graph, int root = 0) : graph(graph) {
    int n = graph.size();
    parent.assign(n, -1);
    subtree.assign(n, 1);
    id.resize(n);
    inv.resize(n);
    head.resize(n);
    dfs1(root);
    head[root] = root;
    int now_id = 0;
    dfs2(root, now_id);
  }

  template <typename Fn>
  void v_update(int u, int v, Fn f) const {
    while (true) {
      if (id[u] > id[v]) std::swap(u, v);
      f(std::max(id[head[v]], id[u]), id[v] + 1);
      if (head[u] == head[v]) return;
      v = parent[head[v]];
    }
  }

  template <typename T, typename F, typename G>
  T v_query(int u, int v, F f, G g, const T ID) const {
    T left = ID, right = ID;
    while (true) {
      if (id[u] > id[v]) {
        std::swap(u, v);
        std::swap(left, right);
      }
      left = g(left, f(std::max(id[head[v]], id[u]), id[v] + 1));
      if (head[u] == head[v]) break;
      v = parent[head[v]];
    }
    return g(left, right);
  }

  template <typename Fn>
  void subtree_v_update(int ver, Fn f) const { f(id[ver], id[ver] + subtree[ver]); }

  template <typename T, typename Fn>
  T subtree_v_query(int ver, Fn f) const { return f(id[ver], id[ver] + subtree[ver]); }

  template <typename Fn>
  void e_update(int u, int v, Fn f) const {
    while (true) {
      if (id[u] > id[v]) std::swap(u, v);
      if (head[u] == head[v]) {
        f(id[u], id[v]);
        break;
      } else {
        f(id[head[v]] - 1, id[v]);
        v = parent[head[v]];
      }
    }
  }

  template <typename T, typename F, typename G>
  T e_query(int u, int v, F f, G g, const T ID) const {
    T left = ID, right = ID;
    while (true) {
      if (id[u] > id[v]) {
        std::swap(u, v);
        std::swap(left, right);
      }
      if (head[u] == head[v]) {
        left = g(left, f(id[u], id[v]));
        break;
      } else {
        left = g(left, f(id[head[v]] - 1, id[v]));
        v = parent[head[v]];
      }
    }
    return g(left, right);
  }

  template <typename Fn>
  void subtree_e_update(int ver, Fn f) const { f(id[ver], id[ver] + subtree[ver] - 1); }

  template <typename T, typename Fn>
  T subtree_e_query(int ver, Fn f) const { return f(id[ver], id[ver] + subtree[ver] - 1); }

  int lowest_common_ancestor(int u, int v) const {
    while (true) {
      if (id[u] > id[v]) std::swap(u, v);
      if (head[u] == head[v]) return u;
      v = parent[head[v]];
    }
  }

private:
  std::vector<std::vector<int>> graph;

  void dfs1(int ver) {
    for (int &e : graph[ver]) {
      if (e != parent[ver]) {
        parent[e] = ver;
        dfs1(e);
        subtree[ver] += subtree[e];
        if (subtree[e] > subtree[graph[ver].front()]) std::swap(e, graph[ver].front());
      }
    }
  }

  void dfs2(int ver, int &now_id) {
    id[ver] = now_id++;
    inv[id[ver]] = ver;
    for (int e : graph[ver]) {
      if (e != parent[ver]) {
        head[e] = (e == graph[ver].front() ? head[ver] : e);
        dfs2(e, now_id);
      }
    }
  }
};

template <typename Abelian>
struct FenwickTreeSupportingRangeAddQuery {
  FenwickTreeSupportingRangeAddQuery(int n_, const Abelian ID = 0) : n(n_), ID(ID) {
    ++n;
    dat_const.assign(n, ID);
    dat_linear.assign(n, ID);
  }

  void add(int left, int right, Abelian val) {
    if (right < ++left) return;
    for (int i = left; i < n; i += i & -i) {
      dat_const[i] -= val * (left - 1);
      dat_linear[i] += val;
    }
    for (int i = right + 1; i < n; i += i & -i) {
      dat_const[i] += val * right;
      dat_linear[i] -= val;
    }
  }

  Abelian sum(int idx) const {
    Abelian res = ID;
    for (int i = idx; i > 0; i -= i & -i) res += dat_linear[i];
    res *= idx;
    for (int i = idx; i > 0; i -= i & -i) res += dat_const[i];
    return res;
  }

  Abelian sum(int left, int right) const {
    return left < right ? sum(right) - sum(left) : ID;
  }

  Abelian operator[](const int idx) const { return sum(idx, idx + 1); }

private:
  int n;
  const Abelian ID;
  std::vector<Abelian> dat_const, dat_linear;
};

int main() {
  int n, k; cin >> n >> k;
  vector<vector<int>> graph(n);
  REP(_, n - 1) {
    int u, v; cin >> u >> v; --u; --v;
    graph[u].emplace_back(v);
    graph[v].emplace_back(u);
  }
  vector<int> d(k); REP(i, k) cin >> d[i], --d[i];
  LowestCommonAncestor lca(graph, d.front());
  sort(ALL(d), [&](int a, int b) -> bool { return lca.left[a] < lca.right[b]; });
  ll steiner = 0;
  REP(i, k) steiner += lca.depth[d[i]] + lca.depth[d[(i + 1) % k]] - lca.depth[lca.query(d[i], d[(i + 1) % k])] * 2;
  HeavyLightDecomposition hld(graph, d.front());
  FenwickTreeSupportingRangeAddQuery<ll> bit(n);
  REP(i, k) hld.v_update(d[i], d[(i + 1) % k], [&](int u, int v) -> void { bit.add(u, v, 1); });
  vector<int> dist(n, -1), from(n, -1);
  queue<int> que;
  REP(i, n) {
    if (bit[i] > 0) {
      dist[i] = 0;
      from[i] = i;
      que.emplace(i);
    }
  }
  while (!que.empty()) {
    int ver = que.front(); que.pop();
    for (int e : graph[ver]) {
      if (dist[e] == -1) {
        dist[e] = dist[ver] + 1;
        from[e] = from[ver];
        que.emplace(e);
      }
    }
  }
  vector<int> has_acorn(n, false);
  REP(i, k) has_acorn[d[i]] = true;
  vector<vector<pair<int, int>>> child(n);
  auto f = [&](auto&& f, int par, int ver) -> void {
    for (int e : graph[ver]) {
      if (e == par) continue;
      f(f, ver, e);
      if (!child[e].empty()) {
        child[ver].emplace_back(child[e].front().first + 1, e);
      } else if (has_acorn[e]) {
        child[ver].emplace_back(1, e);
      }
    }
    sort(ALL(child[ver]), greater{});
  };
  f(f, -1, d.front());
  auto g = [&](auto&& g, int par, int ver, int pd) -> void {
    if (par != -1) {
      child[ver].emplace_back(pd, par);
      sort(ALL(child[ver]), greater{});
    }
    for (int e : graph[ver]) {
      if (e == par) continue;
      if (child[ver].empty()) {
        assert(has_acorn[ver]);
        g(g, ver, e, 1);
      } else if (child[ver].front().second == e) {
        if (child[ver].size() == 1) {
          assert(has_acorn[ver]);
          g(g, ver, e, 1);
        } else {
          g(g, ver, e, child[ver][1].first + 1);
        }
      } else {
        g(g, ver, e, child[ver].front().first + 1);
      }
    }
  };
  g(g, -1, d.front(), 0);
  REP(i, n) cout << steiner + dist[i] - (child[from[i]].empty() ? 0 : child[from[i]].front().first) << '\n';
  return 0;
}
0