結果

問題 No.1075 木の上の山
ユーザー kcvlexkcvlex
提出日時 2020-06-06 03:41:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 101 ms / 2,000 ms
コード長 10,887 bytes
コンパイル時間 1,954 ms
コンパイル使用メモリ 163,616 KB
実行使用メモリ 58,752 KB
最終ジャッジ日時 2024-06-01 06:49:22
合計ジャッジ時間 4,541 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 4 ms
6,940 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 4 ms
6,940 KB
testcase_11 AC 4 ms
6,940 KB
testcase_12 AC 4 ms
6,940 KB
testcase_13 AC 5 ms
6,940 KB
testcase_14 AC 4 ms
6,940 KB
testcase_15 AC 5 ms
6,940 KB
testcase_16 AC 4 ms
6,944 KB
testcase_17 AC 95 ms
58,624 KB
testcase_18 AC 97 ms
58,496 KB
testcase_19 AC 96 ms
58,752 KB
testcase_20 AC 97 ms
58,496 KB
testcase_21 AC 96 ms
58,496 KB
testcase_22 AC 96 ms
58,496 KB
testcase_23 AC 97 ms
58,496 KB
testcase_24 AC 95 ms
58,368 KB
testcase_25 AC 96 ms
58,496 KB
testcase_26 AC 97 ms
58,496 KB
testcase_27 AC 99 ms
58,624 KB
testcase_28 AC 96 ms
58,496 KB
testcase_29 AC 101 ms
58,496 KB
testcase_30 AC 98 ms
58,624 KB
testcase_31 AC 99 ms
58,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define CPP17
#include <limits>
#include <initializer_list>
#include <utility>
#include <bitset>
#include <tuple>
#include <type_traits>
#include <functional>
#include <string>
#include <array>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <iterator>
#include <algorithm>
#include <complex>
#include <random>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <regex>
#include <cassert>
#include <cstddef>
#ifdef CPP17
#include <variant>
#endif

// Yay!!
#define endl codeforces

// macros for iterator
#define ALL(v) std::begin(v), std::end(v)
#define ALLR(v) std::rbegin(v), std::rend(v)

// alias
using ll = std::int64_t;
using ull = std::uint64_t;
using pii = std::pair<int, int>;
using tii = std::tuple<int, int, int>;
using pll = std::pair<ll, ll>;
using tll = std::tuple<ll, ll, ll>;
template <typename T> using vec = std::vector<T>;
template <typename T> using vvec = vec<vec<T>>;

// variadic min/max
template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); }

// variadic chmin/chmax
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }

// multi demension array
template <typename T, std::size_t Head, std::size_t... Tail> struct multi_dim_array { using type = std::array<typename multi_dim_array<T, Tail...>::type, Head>; };
template <typename T, std::size_t Head> struct multi_dim_array<T, Head> { using type = std::array<T, Head>; };
template <typename T, std::size_t... Args> using mdarray = typename multi_dim_array<T, Args...>::type;

#ifdef CPP17
// fill container
template <typename T, typename F, typename... Args> 
void fill_seq(T &t, F f, Args... args) { if constexpr (std::is_invocable<F, Args...>::value) { t = f(args...); } else { for (ssize_t i = 0; i < t.size(); i++) fill_seq(t[i], f, args..., i); } }
#endif

// make multi dimension vector
template <typename T> vec<T> make_v(ssize_t sz) { return vec<T>(sz); }
template <typename T, typename... Tail> auto make_v(ssize_t hs, Tail&&... ts) { auto v = std::move(make_v<T>(std::forward<Tail>(ts)...)); return vec<decltype(v)>(hs, v); }

// init
namespace init__ { 
struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; 
}


namespace math {

template <typename T>
constexpr T pow(const T &n, ll k) {
    T ret = n.mul_id_ele();
    T cur = n;
    while (k) {
        if (k & 1) ret *= cur;
        cur *= cur;
        k /= 2;
    }
    return ret;
}

}

namespace math {

template <ll Mod>
struct Modint {

    constexpr Modint(ll x) noexcept : x((Mod + x % Mod) % Mod) { }
    
    constexpr Modint() noexcept : Modint(0) { }
    
    constexpr Modint<Mod> add_id_ele() const noexcept { 
        return Modint<Mod>(0); 
    }
    
    constexpr Modint<Mod> mul_id_ele() const noexcept {
        return Modint<Mod>(1); 
    }
    
    constexpr ll& value() noexcept { 
        return x; 
    }
    
    constexpr ll value() const noexcept {
        return x; 
    }

    constexpr Modint& operator +=(const Modint &oth) noexcept {
        x += oth.value();
        if (Mod <= x) x -= Mod;
        return *this;
    }

    constexpr Modint& operator -=(const Modint &oth) noexcept {
        x += Mod - oth.value();
        if (Mod <= x) x -= Mod;
        return *this;
    }

    constexpr Modint& operator *=(const Modint &oth) noexcept {
        x *= oth.value();
        x %= Mod;
        return *this;
    }

    constexpr Modint& operator /=(const Modint &oth) noexcept {
        x *= oth.inv().value();
        x %= Mod;
        return *this;
    }

    constexpr Modint operator +(const Modint &oth) const noexcept {
        return Modint(x) += oth;
    }

    constexpr Modint operator -(const Modint &oth) const noexcept {
        return Modint(x) -= oth;
    }

    constexpr Modint operator *(const Modint &oth) const noexcept {
        return Modint(x) *= oth;
    }

    constexpr Modint operator /(const Modint &oth) const noexcept {
        return Modint(x) /= oth;
    }

    constexpr Modint operator -() const noexcept {
        return Modint((x != 0) * (Mod - x)); 
    }

    template <typename T>
    constexpr typename std::enable_if<std::is_integral<T>::value, const Modint&>::type
    operator =(T t) noexcept {
        (*this) = Modint(std::forward<T>(t)); 
        return *this;
    }

    constexpr Modint inv() const noexcept {
        return ::math::pow(*this, Mod - 2);
    }

    constexpr ll mod() const noexcept {
        return Mod;
    }

private:
    ll x;
};

}

namespace graph {

using Node = ll;
using Weight = ll;
using Edge = std::pair<Node, Weight>;

template <bool Directed>
struct Graph : public vvec<Edge> {
    using vvec<Edge>::vvec;

    void add_edge(Node f, Node t, Weight w = 1) {
        (*this)[f].emplace_back(t, w);
        if (!Directed) (*this)[t].emplace_back(f, w);
    }

    Graph<Directed> build_inv() const {
        Graph<Directed> ret(this->size());
        for (Node i = 0; i < this->size(); i++) {
            for (const Edge &e : (*this)[i]) {
                Node j;
                Weight w;
                std::tie(j, w) = e;
                if (!Directed && j < i) continue;
                ret.add_edge(j, i, w);
            }
        }

        return ret;
    }
};

template <typename Iterator>
class dst_iterator {
    Iterator ite;

public:
    dst_iterator(Iterator ite) : ite(ite) { }

    bool operator ==(const dst_iterator<Iterator> &oth) const {
        return ite == oth.ite;
    }

    bool operator !=(const dst_iterator<Iterator> &oth) const {
        return !(*this == oth);
    }

    bool operator <(const dst_iterator<Iterator> &oth) const {
        return ite < oth.ite;
    }

    bool operator >(const dst_iterator<Iterator> &oth) const {
        return ite > oth.ite;
    }

    bool operator <=(const dst_iterator<Iterator> &oth) const {
        return ite <= oth.ite;
    }

    bool operator >=(const dst_iterator<Iterator> &oth) const {
        return ite >= oth.ite;
    }

    const Node& operator *() {
        return ite->first;
    }

    const Node& operator *() const {
        return ite->first;
    }

    dst_iterator operator ++() {
        ++ite;
        return ite;
    }
};

class dst_iteration {
    using ite_type = vec<Edge>::const_iterator;
    const vec<Edge> &edges;

public:
    dst_iteration(const vec<Edge> &edges) : edges(edges) { }

    auto begin() const {
        return dst_iterator<ite_type>(edges.cbegin());
    }

    auto end() const {
        return dst_iterator<ite_type>(edges.cend());
    }
};

class dst_reverse_iteration {
    using ite_type = vec<Edge>::const_reverse_iterator;
    const vec<Edge> &edges;

public:
    dst_reverse_iteration(const vec<Edge> &edges) : edges(edges) { }

    auto begin() const {
        return dst_iterator<ite_type>(edges.crbegin());
    }

    auto end() const {
        return dst_iterator<ite_type>(edges.crend());
    }
};

dst_iteration dst(const vec<Edge> &edges) {
    return dst_iteration(edges);
}

dst_reverse_iteration rdst(const vec<Edge> &edges) {
    return dst_reverse_iteration(edges);
}

}

constexpr ll mod = 1e9 + 7;
using mint = math::Modint<mod>;

struct Solver {
    graph::Graph<false> tree;
    ll k;
    vec<ll> par;
    vvec<mint> dp, rdp, dp_sum;
    vec<vvec<mint>> lsum, rsum;
    ll root = 1;

    Solver(ll n, ll k) 
        : par(n), tree(n), k(k), 
          dp(make_v<mint>(n, k + 1)), rdp(dp),
          dp_sum(make_v<mint>(n, k + 2)), 
          lsum(n), rsum(n)
    {
        for (ll i = 1; i < n; i++) {
            ll a, b;
            std::cin >> a >> b;
            tree.add_edge(a - 1, b - 1);
        }
    }

    void dfs(ll cur, ll pre) {
        par[cur] = pre;
        dp[cur][0] = 0;
        ll cnt = 0;
        fill_seq(dp[cur], [](ll i) { return mint(!!i); });
       
        for (ll nxt : graph::dst(tree[cur])) if (pre != nxt) {
            dfs(nxt, cur);
            cnt++;
            for (ll i = 1; i <= k; i++) dp[cur][i] *= dp_sum[nxt][i + 1];
        }
        
        lsum[cur] = make_v<mint>(cnt + 1, k + 1);
        rsum[cur] = make_v<mint>(cnt + 1, k + 1);
        for (ll loop = 0; loop < 2; loop++) {
            auto &sum = (loop == 0 ? lsum[cur] : rsum[cur]);
            std::fill(ALL(sum[0]), mint(1));
            ll idx = 0;
            fill_seq(sum[0], [](int) { return mint(1); });

            auto update = [&](ll nxt) {
                if (nxt == pre) return;
                for (ll i = 1; i <= k; i++) sum[idx + 1][i] += sum[idx][i] * dp_sum[nxt][i + 1];
                idx++;
            };
            
            if (loop == 0) {
                for (ll nxt : graph::dst(tree[cur])) update(nxt);
            } else { 
                for (ll nxt : graph::rdst(tree[cur])) update(nxt);
            }

            for (ll i = 1; i <= cnt; i++) {
                mint tmp = 0;
                for (ll j = 2; j <= k; j++) {
                    tmp += sum[i][j - 1];
                    sum[i][j] -= tmp;
                }
            }
        }

        for (ll i = 1; i <= k; i++) dp_sum[cur][i + 1] = dp_sum[cur][i] + dp[cur][i];
    }

    void calc_rdp(ll cur, ll pidx) {
        if (cur == root) {
            fill_seq(rdp[cur], [](ll i) { return mint(!!i); });
        } else {
            mint ls = 0, rs = 0, psum = 0;
            ll p = par[cur];
            ll ch = lsum[p].size() - 1;
            if (pidx == 0) ls = 1;
            if (pidx == ch - 1) rs = 1;
            for (ll i = 1; i <= k; i++) {
                if (pidx != 0) ls += lsum[p][pidx][i];
                if (ch - pidx - 1 != 0) rs += rsum[p][ch - pidx - 1][i];
                psum += ls * rs * rdp[p][i];
                rdp[cur][i] = psum;
            }
        }
        ll idx = 0;
        for (ll nxt : graph::dst(tree[cur])) if (nxt != par[cur]) calc_rdp(nxt, idx++);
    }

    mint solve() {
        dfs(root, -1);
        calc_rdp(root, -1);
        mint ans = 0;
        for (ll n = 0; n < tree.size(); n++) for (ll i = 1; i <= k; i++) {
            if (n == root) ans += dp[n][i];
            else ans += dp[n][i] * rdp[n][i - 1];
        }
        return ans;
    }
};

int main() {
    ll n, k;
    std::cin >> n >> k;
    Solver solver(n, k);
    std::cout << solver.solve().value() << "\n";
    return 0;
}
0