結果
問題 | No.1075 木の上の山 |
ユーザー | kcvlex |
提出日時 | 2020-06-06 03:23:27 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 10,767 bytes |
コンパイル時間 | 2,008 ms |
コンパイル使用メモリ | 162,804 KB |
実行使用メモリ | 58,752 KB |
最終ジャッジ日時 | 2024-06-01 06:11:34 |
合計ジャッジ時間 | 4,900 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | AC | 107 ms
58,624 KB |
testcase_18 | WA | - |
testcase_19 | AC | 106 ms
58,752 KB |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
ソースコード
#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 = 0; 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]; sum[idx + 1][i] -= sum[idx][i - 1] * dp_sum[nxt][i]; } 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 <= 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; }