正文
增加 5 行删除 5 行
123456782 行未改动11121314151617181924 行未改动显然是一个树形dp当然不需要记录贡献,只需要每次求出答案后把值累加到答案数组即可。对于每个结点递推子树中的点到该结点的距离。由于该结点是lca,故而需要把每一个子树分开统计。具体地,每次统计完一个孩子的子树后累加到该结点,再和后面的结点搞。然后就搞出答案了。```cpp#include <cstdio>#include <cstring>#include <algorithm>2 行未改动const int N = 123456, H = 567;int head[N], ver[N], nex[N], tot;inline void addedge(int u, int v) {ver[tot] = v, nex[tot] = head[u], head[u] = tot++;}int dis[N][H], hei[N], ans[H];void dfs(int cur) {24 行未改动123456782 行未改动11121314151617181924 行未改动显然是一个树形dp当然不需要记录贡献,只需要每次求出答案后把值累加到答案数组即可。对于每个结点递推子树中的点到该结点的距离。由于该结点是lca,故而需要把每一个子树分开统计。具体地,每次统计完一个孩子的子树后累加到该结点,再和后面的结点搞。然后就搞出答案了。```cpp#include <cstdio>#include <cstring>#include <algorithm>2 行未改动const int N = 123456, H = 567;int head[N], ver[N], nex[N], tot;inline void addedge(int u, int v) {ver[tot] = v, nex[tot] = head[u], head[u] = tot++;}int dis[N][H], hei[N], ans[H];void dfs(int cur) {24 行未改动