結果

問題 No.1308 ジャンプビーコン
ユーザー PachicobuePachicobue
提出日時 2020-12-05 03:03:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,584 ms / 4,000 ms
コード長 16,631 bytes
コンパイル時間 3,963 ms
コンパイル使用メモリ 246,272 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-13 13:27:17
合計ジャッジ時間 28,659 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 3 ms
4,356 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 3 ms
4,352 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 74 ms
4,348 KB
testcase_14 AC 74 ms
4,348 KB
testcase_15 AC 75 ms
4,348 KB
testcase_16 AC 73 ms
4,348 KB
testcase_17 AC 72 ms
4,352 KB
testcase_18 AC 1,488 ms
4,356 KB
testcase_19 AC 1,491 ms
4,348 KB
testcase_20 AC 1,463 ms
4,352 KB
testcase_21 AC 1,450 ms
4,348 KB
testcase_22 AC 1,430 ms
4,348 KB
testcase_23 AC 1 ms
4,352 KB
testcase_24 AC 4 ms
4,352 KB
testcase_25 AC 3 ms
4,348 KB
testcase_26 AC 950 ms
4,352 KB
testcase_27 AC 942 ms
4,348 KB
testcase_28 AC 938 ms
4,348 KB
testcase_29 AC 1,162 ms
4,352 KB
testcase_30 AC 1,184 ms
4,352 KB
testcase_31 AC 1,146 ms
4,348 KB
testcase_32 AC 1,024 ms
4,352 KB
testcase_33 AC 1,302 ms
4,348 KB
testcase_34 AC 982 ms
4,352 KB
testcase_35 AC 963 ms
4,348 KB
testcase_36 AC 1,105 ms
4,348 KB
testcase_37 AC 1,116 ms
4,348 KB
testcase_38 AC 1,546 ms
4,352 KB
testcase_39 AC 1,584 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using ld = long double;
template<typename T> using max_heap = std::priority_queue<T>;
template<typename T> using min_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
constexpr int popcount(const ull v) { return v ? __builtin_popcountll(v) : 0; }
constexpr int log2p1(const ull v) { return v ? 64 - __builtin_clzll(v) : 0; }
constexpr int lsbp1(const ull v) { return __builtin_ffsll(v); }
constexpr int clog(const ull v) { return v ? log2p1(v - 1) : 0; }
constexpr ull ceil2(const ull v) { return 1ULL << clog(v); }
constexpr ull floor2(const ull v) { return v ? (1ULL << (log2p1(v) - 1)) : 0ULL; }
constexpr bool btest(const ull mask, const int ind) { return (mask >> ind) & 1ULL; }
template<typename T> void bset(T& mask, const int ind) { mask |= ((T)1 << ind); }
template<typename T> void breset(T& mask, const int ind) { mask &= ~((T)1 << ind); }
template<typename T> void bflip(T& mask, const int ind) { mask ^= ((T)1 << ind); }
template<typename T> void bset(T& mask, const int ind, const bool b) { (b ? bset(mask, ind) : breset(mask, ind)); }
template<typename SemiGroup>
class ds_table
{
    using T = typename SemiGroup::T;
public:
    ds_table(const std::vector<T>& vs) : m_size{(int)vs.size()}, m_depth{(int)log2p1(m_size)}, m_vss(m_depth, vs), m_merge{}
    {
        for (int d = 0; d < m_depth; d++) {
            const int width = 1 << (m_depth - d - 1);
            for (int i = 1; i * width < m_size; i += 2) {
                int l = i * width - 1, r = i * width;
                for (int j = 1; j < width; j++) {
                    m_vss[d][l - j] = m_merge(vs[l - j], m_vss[d][l - j + 1]);
                    if (r + j < m_size) { m_vss[d][r + j] = m_merge(vs[r + j], m_vss[d][r + j - 1]); }
                }
            }
        }
    }
    T fold(const int l, int r) const
    {
        assert(0 <= l and l < r and r <= m_size);
        if (r - l == 1) { return m_vss.back()[l]; }
        r--;
        const int d = m_depth - log2p1(l ^ r);
        return m_merge(m_vss[d][l], m_vss[d][r]);
    }
private:
    const int m_size, m_depth;
    std::vector<std::vector<T>> m_vss;
    const SemiGroup m_merge;
};
template<typename TotalOrd, typename B = ull>
class static_rmq
{
    using T = typename TotalOrd::T;
public:
    static_rmq(const std::vector<T>& vs)
        : m_comp{}, m_sz{(int)vs.size()}, m_bn{(int)wind(m_sz + bs - 1)}, m_vals{vs}, m_bucket_vals([&]() {
              std::vector<T> ans(m_bn);
              for (int i = 0; i < m_sz; i++) { ans[wind(i)] = i % bs == 0 ? m_vals[i] : std::min(ans[wind(i)], m_vals[i], m_comp); }
              return ans;
          }()),
          m_masks(m_sz, 0),
          m_st(m_bucket_vals)
    {
        for (int i = 0; i < m_bn; i++) {
            std::vector<int> g(bs, m_sz);
            std::stack<int> stack;
            for (int j = 0; j < bs and (int) ind(i, j) < m_sz; j++) {
                for (; not stack.empty() and not m_comp(m_vals[stack.top()], m_vals[ind(i, j)]); stack.pop()) {}
                g[j] = stack.empty() ? m_sz : stack.top(), stack.push(ind(i, j));
            }
            for (int j = 0; j < bs and (int) ind(i, j) < m_sz; j++) { m_masks[ind(i, j)] = g[j] == m_sz ? static_cast<B>(0) : (m_masks[g[j]] | static_cast<B>(1) << (g[j] - i * bs)); }
        }
    }
    T fold(const int l, const int r) const
    {
        assert(0 <= l and l < r and r <= m_sz);
        const int lb = (l + bs - 1) / bs, rb = r / bs;
        if (lb > rb) {
            return brmq(l, r);
        } else {
            return lb < rb
                       ? (l < bs * lb
                              ? (bs * rb < r
                                     ? std::min({m_st.fold(lb, rb), brmq(l, bs * lb), brmq(bs * rb, r)}, m_comp)
                                     : std::min(m_st.fold(lb, rb), brmq(l, bs * lb), m_comp))
                              : (bs * rb < r
                                     ? std::min(m_st.fold(lb, rb), brmq(bs * rb, r), m_comp)
                                     : m_st.fold(lb, rb)))
                       : (l < bs * lb
                              ? (bs * rb < r
                                     ? std::min(brmq(l, bs * lb), brmq(bs * rb, r), m_comp)
                                     : brmq(l, bs * lb))
                              : (bs * rb < r
                                     ? brmq(bs * rb, r)
                                     : T{}));
        }
    }
private:
    static constexpr int bs = sizeof(B) * 8;
    static constexpr int bslog = log2p1(bs) - 1;
    static constexpr uint wind(const uint n) { return n >> (bslog); }
    static constexpr uint bind(const uint n) { return n ^ (wind(n) << bslog); }
    static constexpr uint ind(const uint w, const uint b) { return (w << bslog) | b; }
    T brmq(const int l, const int r) const
    {
        const B w = m_masks[r - 1] >> (l % bs);
        return w == 0 ? m_vals[r - 1] : m_vals[l + lsbp1(w) - 1];
    }
    struct SemiGroup
    {
        using T = typename TotalOrd::T;
        T operator()(const T& x1, const T& x2) const { return std::min(x1, x2, TotalOrd{}); }
    };
    const TotalOrd m_comp;
    int m_sz, m_bn;
    std::vector<T> m_vals, m_bucket_vals;
    std::vector<B> m_masks;
    ds_table<SemiGroup> m_st;
};
template<typename T = int>
class graph
{
public:
    graph(const int size) : m_size{size}, m_eis(m_size) {}
    void add_edge(const int u, const int v, const T& c, const bool bi = false)
    {
        const int ei = (int)m_us.size();
        m_eis[u].push_back(ei);
        if (bi) { m_eis[v].push_back(ei); }
        m_us.push_back(u), m_vs.push_back(v), m_cs.push_back(c);
    }
    void add_edge(const int u, const int v, const bool bi = false) { add_edge(u, v, 1, bi); }
    const std::vector<int>& operator[](const int u) const { return m_eis[u]; }
    std::vector<int>& operator[](const int u) { return m_eis[u]; }
    std::tuple<int, int, T> edge(const int u, const int i) const { return std::make_tuple(u, m_us[i] ^ m_vs[i] ^ u, m_cs[i]); }
    int size() const { return m_size; }
    friend std::ostream& operator<<(std::ostream& os, const graph& g)
    {
        for (int u = 0; u < g.size(); u++) {
            for (const int ei : g[u]) {
                const auto& [from, to, cost] = g.edge(u, ei);
                os << "[" << ei << "]: " << from << "->" << to << "(" << cost << ")\n";
            }
        }
        return os;
    }
private:
    int m_size;
    std::vector<std::vector<int>> m_eis;
    std::vector<int> m_us, m_vs;
    std::vector<T> m_cs;
};
template<typename T>
class hl_decomp
{
public:
    hl_decomp(graph<T>& g, const int r = 0) : m_pars(g.size(), -1), m_tops{m_pars}, m_ins{m_pars}, m_ords{m_pars}, m_outs{m_pars}
    {
        const int N = g.size();
        std::vector<int> szs(N, 1);
        auto dfs1 = [&](auto self, const int u, const int p) -> int {
            m_pars[u] = p;
            for (int& ei : g[u]) {
                [[maybe_unused]] const auto& [from, to, cost] = g.edge(u, ei);
                if (p == to) { continue; }
                szs[u] += self(self, to, u);
                if (szs[std::get<1>(g.edge(u, g[u][0]))] < szs[to]) { std::swap(g[u][0], ei); }
            }
            return szs[u];
        };
        dfs1(dfs1, r, -1);
        m_tops[r] = r;
        auto dfs2 = [&](auto&& self, const int u, const int p, int& ind) -> void {
            m_ins[u] = ind++, m_ords[m_ins[u]] = u;
            for (const int ei : g[u]) {
                [[maybe_unused]] const auto& [from, to, cost] = g.edge(u, ei);
                if (to == p) { continue; }
                m_tops[to] = (ei == g[u][0] ? m_tops[u] : to);
                self(self, to, u, ind);
            }
            m_outs[u] = ind;
        };
        int ind = 0;
        dfs2(dfs2, r, -1, ind);
    }
    int pos(const int v) const { return m_ins[v]; }
    int at(const int n) const { return m_ords[n]; }
    std::pair<int, int> sub(const int v) const { return {m_ins[v], m_outs[v]}; }
    std::vector<std::pair<int, int>> path(int u, int v) const
    {
        using P = std::pair<int, int>;
        std::vector<P> head, tail;
        for (int pu = m_tops[u], pv = m_tops[v]; pu != pv;) {
            if (m_ins[pu] < m_ins[pv]) {
                tail.push_back({m_ins[pv], m_ins[v]});
                v = m_pars[pv], pv = m_tops[v];
            } else {
                head.push_back({m_ins[u], m_ins[pu]});
                u = m_pars[pu], pu = m_tops[u];
            }
        }
        head.push_back({m_ins[u], m_ins[v]});
        std::reverse(tail.begin(), tail.end());
        for (const auto& p : tail) { head.push_back(p); }
        return head;
    }
    friend std::ostream& operator<<(std::ostream& os, const hl_decomp& hld)
    {
        os << "ord = {";
        for (const int v : hld.m_ords) { os << v << ","; }
        os << "}\ntops = {";
        for (const int v : hld.m_tops) { os << v << ","; }
        return os << "}";
    }
private:
    std::vector<int> m_pars, m_tops, m_ins, m_ords, m_outs;
};
template<typename C>
class lca_manager
{
public:
    lca_manager(const graph<C>& g, const int r = 0) : m_ins(g.size(), 0),
                                                      m_ds([&]() {
                                                          std::vector<std::pair<int, int>> ans;
                                                          std::vector<bool> used(g.size(), false);
                                                          auto dfs = [&](auto self, const std::pair<int, int>& s) -> void {
                                                              const int pos = s.second;
                                                              used[pos] = true, m_ins[pos] = ans.size(), ans.push_back(s);
                                                              for (const int ei : g[pos]) {
                                                                  [[maybe_unused]] const auto& [from, to, cost] = g.edge(pos, ei);
                                                                  if (used[to]) { continue; }
                                                                  self(self, {s.first + 1, to}), ans.push_back(s);
                                                              }
                                                          };
                                                          dfs(dfs, {0, r});
                                                          return ans;
                                                      }()),
                                                      m_rmq(m_ds) {}
    int operator()(const int u, const int v) const
    {
        const int ul = m_ins[u], vl = m_ins[v];
        return m_rmq.fold(std::min(ul, vl), std::max(ul, vl) + 1).second;
    }
private:
    struct Ord
    {
        using T = std::pair<int, int>;
        bool operator()(const T& p1, const T& p2) const { return p1 < p2; }
    };
    std::vector<int> m_ins;
    std::vector<std::pair<int, int>> m_ds;
    static_rmq<Ord> m_rmq;
};
template<typename T> bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); }
template<typename T> bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); }
template<typename T> constexpr T inf_v = std::numeric_limits<T>::max() / 4;
template<typename Real> constexpr Real pi_v = Real{3.141592653589793238462643383279502884};
template<typename T> constexpr T TEN(const int n) { return n == 0 ? T{1} : TEN<T>(n - 1) * T{10}; }
template<typename F> struct fix : F
{
    fix(F&& f) : F{std::forward<F>(f)} {}
    template<typename... Args> auto operator()(Args&&... args) const { return F::operator()(*this, std::forward<Args>(args)...); }
};
template<typename T, int n, int i = 0>
auto nd_array(int const (&szs)[n], const T x = T{})
{
    if constexpr (i == n) {
        return x;
    } else {
        return std::vector(szs[i], nd_array<T, n, i + 1>(szs, x));
    }
}
class printer
{
public:
    printer(std::ostream& os_ = std::cout) : m_os{os_} { m_os << std::fixed << std::setprecision(15); }
    template<typename... Args> int ln(const Args&... args) { return dump(args...), m_os << '\n', 0; }
    template<typename... Args> int el(const Args&... args) { return dump(args...), m_os << std::endl, 0; }
private:
    template<typename T> void dump(const T& v) { m_os << v; }
    template<typename T> void dump(const std::vector<T>& vs)
    {
        for (int i = 0; i < (int)vs.size(); i++) { m_os << (i ? " " : ""), dump(vs[i]); }
    }
    template<typename T> void dump(const std::vector<std::vector<T>>& vss)
    {
        for (int i = 0; i < (int)vss.size(); i++) { m_os << (0 <= i or i + 1 < (int)vss.size() ? "\n" : ""), dump(vss[i]); }
    }
    template<typename T, typename... Args> int dump(const T& v, const Args&... args) { return dump(v), m_os << ' ', dump(args...), 0; }
    std::ostream& m_os;
};
printer out;
class scanner
{
public:
    scanner(std::istream& is_ = std::cin) : m_is{is_} { m_is.tie(nullptr), std::ios::sync_with_stdio(false); }
    void fastio(const bool on)
    {
        if (on) {
            m_is.tie(nullptr), std::ios::sync_with_stdio(false);
        } else {
            m_is.tie(&std::cout), std::ios::sync_with_stdio(true);
        }
    }
    template<typename T> T val()
    {
        T v;
        return m_is >> v, v;
    }
    template<typename T> T val(const T offset) { return val<T>() - offset; }
    template<typename T> std::vector<T> vec(const int n)
    {
        return make_v<T>(n, [this]() { return val<T>(); });
    }
    template<typename T> std::vector<T> vec(const int n, const T offset)
    {
        return make_v<T>(n, [this, offset]() { return val<T>(offset); });
    }
    template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1)
    {
        return make_v<std::vector<T>>(n0, [this, n1]() { return vec<T>(n1); });
    }
    template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1, const T offset)
    {
        return make_v<std::vector<T>>(n0, [this, n1, offset]() { return vec<T>(n1, offset); });
    }
    template<typename... Args> auto tup() { return std::tuple<std::decay_t<Args>...>{val<Args>()...}; }
    template<typename... Args> auto tup(const Args&... offsets) { return std::tuple<std::decay_t<Args>...>{val<Args>(offsets)...}; }
private:
    template<typename T, typename F>
    std::vector<T> make_v(const int n, F f)
    {
        std::vector<T> ans;
        for (int i = 0; i < n; i++) { ans.push_back(f()); }
        return ans;
    }
    std::istream& m_is;
};
scanner in;
int main()
{
    const auto [N, Q, C] = in.tup<int, int, ll>();
    graph<ll> g(N);
    for (int i = 0; i < N - 1; i++) {
        const auto [u, v, c] = in.tup<int, int, ll>(1, 1, 0);
        g.add_edge(u, v, c, true);
    }
    auto xs = in.vec<int>(Q, 1);
    xs.push_back(0);
    std::vector<int> ps(N, -1);
    std::vector<ll> ds(N, 0);
    fix([&](auto self, const int s, const int p) -> void {
        for (const int ei : g[s]) {
            const auto [from, to, cost] = g.edge(s, ei);
            if (to == p) { continue; }
            ps[to] = from, ds[to] = ds[from] + cost;
            self(to, s);
        }
    })(0, -1);
    const auto lca = lca_manager(g);
    auto dist = [&](const int i, const int j) { return ds[i] + ds[j] - 2LL * ds[lca(i, j)]; };
    struct Ord
    {
        using T = ll;
        bool operator()(const T x, const T y) const { return x < y; }
    };
    std::vector<ll> dp(N, inf_v<ll>);
    dp[xs[0]] = 0;
    static_cast<void>(0);
    for (int i = 1; i < Q; i++) {
        const ll pmin = *std::min_element(dp.begin(), dp.end());
        std::vector<ll> ndp(N, inf_v<ll>);
        const int y = lca(xs[i - 1], xs[i]);
        for (int v = xs[i - 1];; v = ps[v]) {
            ndp[v] = dist(xs[i - 1], xs[i]) + pmin;
            if (v == y) { break; }
        }
        for (int v = xs[i];; v = ps[v]) {
            ndp[v] = dist(xs[i - 1], xs[i]) + pmin;
            if (v == y) { break; }
        }
        for (int j = 0; j < N; j++) { chmin(ndp[j], dp[j] + dist(xs[i], xs[i - 1])); }
        const auto hld = hl_decomp(g, xs[i]);
        std::vector<ll> dp_depth(N, inf_v<ll>);
        for (int j = 0; j < N; j++) { dp_depth[hld.pos(j)] = dp[j] + dist(xs[i], j); }
        const auto rmq = static_rmq<Ord>(dp_depth);
        for (int j = 0; j < N; j++) {
            const auto [l, r] = hld.sub(j);
            chmin(ndp[j], rmq.fold(l, r) + C);
        }
        dp = ndp;
        static_cast<void>(0);
    }
    out.ln(*std::min_element(dp.begin(), dp.end()));
}
0