From 24504d62e2b050fdf0198ed28f720c0b02840d4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Wed, 10 Jan 2024 16:43:15 +0800 Subject: [PATCH] 'commit' --- TangDou/Topic/HuanGenDp/P3047.cpp | 71 ++++++++++++------------------- 1 file changed, 28 insertions(+), 43 deletions(-) diff --git a/TangDou/Topic/HuanGenDp/P3047.cpp b/TangDou/Topic/HuanGenDp/P3047.cpp index ca2d7ef..21de1ec 100644 --- a/TangDou/Topic/HuanGenDp/P3047.cpp +++ b/TangDou/Topic/HuanGenDp/P3047.cpp @@ -1,75 +1,60 @@ #include -#define re register -typedef long long ll; using namespace std; -inline ll read() { - ll a = 0, f = 1; - char c = getchar(); - while (c < '0' || c > '9') { - if (c == '-') f = -1; - c = getchar(); - } - while (c >= '0' && c <= '9') { - a = a * 10 + c - '0'; - c = getchar(); - } - return a * f; -} // 好用的快读 -ll n, q; -ll head[200010], dp[200010][21]; +#define int long long +#define endl "\n" + +int n, q; +int head[200010], dp[200010][21]; struct ljj { - ll to, stb; + int to, stb; } a[200010]; -ll s = 0; -inline void insert(ll x, ll y) { +int s = 0; +inline void insert(int x, int y) { s++; a[s].stb = head[x]; a[s].to = y; head[x] = s; } -inline void dfs(ll x, ll fa) { - for (re ll i = head[x]; i; i = a[i].stb) { - ll xx = a[i].to; +inline void dfs(int x, int fa) { + for (int i = head[x]; i; i = a[i].stb) { + int xx = a[i].to; if (xx == fa) continue; dfs(xx, x); - for (re ll j = 1; j <= q; j++) + for (int j = 1; j <= q; j++) dp[x][j] += dp[xx][j - 1]; // 第一遍dp } } -inline void dfs1(ll x, ll fa) { - for (re ll i = head[x]; i; i = a[i].stb) { - ll xx = a[i].to; - if (xx == fa) - continue; +inline void dfs1(int x, int fa) { + for (int i = head[x]; i; i = a[i].stb) { + int xx = a[i].to; + if (xx == fa) continue; // 在第一次遍历时 dp[1][2] 包括了 dp[2][1] 2的子树权值; // 然鹅 ans在统计dp[2][3] 的时候也加上了 dp[2][1] 2的子树权值; // 第二次遍历 dp[2][3] 又加上了 dp[2][1]; // 所以需要简单容斥一下; - for (re ll j = q; j >= 2; j--) + for (int j = q; j >= 2; j--) dp[xx][j] -= dp[xx][j - 2]; // 简单容斥 - for (re ll j = 1; j <= q; j++) + for (int j = 1; j <= q; j++) dp[xx][j] += dp[x][j - 1]; // 第二遍dp dfs1(xx, x); } } -int main() { - n = read(); - q = read(); - for (re ll i = 1; i < n; i++) { - ll x = read(), y = read(); +signed main() { + cin >> n >> q; + for (int i = 1; i < n; i++) { + int x, y; + cin >> x >> y; insert(x, y); insert(y, x); } - for (re ll i = 1; i <= n; i++) - dp[i][0] = read(); // 每个节点往外0距离,就是它本身的权值; + for (int i = 1; i <= n; i++) cin >> dp[i][0]; // 每个节点往外0距离,就是它本身的权值; dfs(1, 0); dfs1(1, 0); - for (re ll i = 1; i <= n; i++) { - ll ans = 0; - for (re ll j = 0; j <= q; j++) - ans += dp[i][j]; // ans统计答案 + + for (int i = 1; i <= n; i++) { + int ans = 0; + for (int j = 0; j <= q; j++) ans += dp[i][j]; // ans统计答案 printf("%lld\n", ans); } - return 0; } \ No newline at end of file