diff --git a/TangDou/Topic/HuanGenDp/P3047.cpp b/TangDou/Topic/HuanGenDp/P3047.cpp index 21de1ec..ace01db 100644 --- a/TangDou/Topic/HuanGenDp/P3047.cpp +++ b/TangDou/Topic/HuanGenDp/P3047.cpp @@ -4,28 +4,32 @@ using namespace std; #define endl "\n" int n, q; -int head[200010], dp[200010][21]; +const int N = 200010, K = 21; +int head[N], dp[N][K]; struct ljj { int to, stb; -} a[200010]; -int s = 0; -inline void insert(int x, int y) { +} a[N]; +int s; + +void insert(int x, int y) { s++; a[s].stb = head[x]; a[s].to = y; head[x] = s; } -inline void dfs(int x, int fa) { + +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; - dfs(xx, x); + dfs1(xx, x); for (int j = 1; j <= q; j++) dp[x][j] += dp[xx][j - 1]; // 第一遍dp } } -inline void dfs1(int x, int fa) { + +void dfs2(int x, int fa) { for (int i = head[x]; i; i = a[i].stb) { int xx = a[i].to; if (xx == fa) continue; @@ -37,7 +41,7 @@ inline void dfs1(int x, int fa) { dp[xx][j] -= dp[xx][j - 2]; // 简单容斥 for (int j = 1; j <= q; j++) dp[xx][j] += dp[x][j - 1]; // 第二遍dp - dfs1(xx, x); + dfs2(xx, x); } } signed main() { @@ -49,8 +53,8 @@ signed main() { insert(y, x); } for (int i = 1; i <= n; i++) cin >> dp[i][0]; // 每个节点往外0距离,就是它本身的权值; - dfs(1, 0); dfs1(1, 0); + dfs2(1, 0); for (int i = 1; i <= n; i++) { int ans = 0;