結果

問題 No.1075 木の上の山
ユーザー milanis48663220milanis48663220
提出日時 2023-07-12 23:52:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 4,298 bytes
コンパイル時間 1,624 ms
コンパイル使用メモリ 138,028 KB
実行使用メモリ 35,460 KB
最終ジャッジ日時 2023-10-12 13:38:22
合計ジャッジ時間 3,912 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 3 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 3 ms
4,352 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 3 ms
4,352 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 59 ms
35,460 KB
testcase_18 AC 44 ms
14,836 KB
testcase_19 AC 56 ms
30,692 KB
testcase_20 AC 45 ms
14,892 KB
testcase_21 AC 45 ms
12,536 KB
testcase_22 AC 46 ms
12,488 KB
testcase_23 AC 45 ms
12,912 KB
testcase_24 AC 47 ms
12,732 KB
testcase_25 AC 42 ms
12,392 KB
testcase_26 AC 46 ms
14,964 KB
testcase_27 AC 47 ms
13,376 KB
testcase_28 AC 49 ms
20,960 KB
testcase_29 AC 44 ms
13,920 KB
testcase_30 AC 46 ms
15,216 KB
testcase_31 AC 49 ms
20,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <atcoder/modint>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

using mint = atcoder::modint1000000007;

ostream& operator<<(ostream& os, const mint& m){
    os << m.val();
    return os;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, k; cin >> n >> k;
    vector<vector<int>> g(n);
    for(int i = 0; i < n-1; i++){
        int a, b; cin >> a >> b; a--; b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vector<vector<vector<mint>>> dp(n);
    for(int i = 0; i < n; i++){
        dp[i].assign(g[i].size(), vector<mint>(k));
    }
    function<vector<mint>(int, int)> dfs1 = [&](int v, int par){
        vector<mint> ans(k);
        for(int x = 0; x < k; x++) ans[x] = 1;
        for(int i = 0; i < g[v].size(); i++){
            int to = g[v][i];
            if(to == par) continue;
            dp[v][i] = dfs1(to, v);
            for(int x = 0; x < k; x++){
                ans[x] *= dp[v][i][x];
            }
        }
        for(int x = 1; x < k; x++) ans[x] += ans[x-1];
        // cout << "====" << endl;
        // cout << v << ":"; print_vector(ans);
        return ans;
    };
    auto merge = [&](vector<mint> v, vector<mint> u){
        vector<mint> w(k);
        for(int i = 0; i < k; i++) w[i] = v[i]*u[i];
        return w;
    };
    dfs1(0, -1);
    mint ans = 0;
    function<void(int, int, vector<mint>)> dfs2 = [&](int v, int p, vector<mint> from_par){
        int deg = g[v].size();
        for(int i = 0; i < deg; i++){
            if(g[v][i] == p){
                dp[v][i] = from_par;
                break;
            }
        }
        // cout << "=====" << endl;
        if(v == 0){
            // cout << v << ":";
            for(int x = 0; x < k; x++){
                mint prod = 1;
                for(int i = 0; i < deg; i++){
                    prod *= dp[v][i][x];
                }
                // cout << prod << ' ';
                ans += prod;
            }
            // cout << endl;
        }else{
            // cout << v << ":";
            for(int x = 1; x < k; x++){
                mint prod = 1;
                for(int i = 0; i < deg; i++){
                    if(g[v][i] == p){
                        prod *= dp[v][i][x-1];
                    }else{
                        prod *= dp[v][i][x];
                    }
                }
                // cout << prod << ' ';
                ans += prod;
            }
            // cout << endl;
        }
        auto from_right = vec2d(deg+1, k, mint(0));
        from_right[deg] = vector<mint>(k, mint(1));
        for(int i = deg-1; i >= 0; i--){
            from_right[i] = merge(from_right[i+1], dp[v][i]);
        }
        vector<mint> from_left(k, mint(1));
        for(int i = 0; i < deg; i++){
            int u = g[v][i];
            if(u != p){
                auto x = merge(from_left, from_right[i+1]);
                for(int i = 1; i < k; i++) x[i] += x[i-1];
                dfs2(u, v, x);
            }
            from_left = merge(from_left, dp[v][i]);
        }
    };
    dfs2(0, -1, vector<mint>(k, mint(1)));
    cout << ans << endl;
}
0