7 条题解
-
-3
和 T1 一样,考虑拆每个边权的答案贡献
固定边权 ,删掉树上所有边权为 的边。剩下的每个连通块缩成一个点,被删掉的边就成为缩点树上的边
两个点之间的原树路径上,边权 出现的次数,就是对应缩点树上的路径长度。因此边权 在路径上恰好出现一次,当且仅当两端所在的连通块在缩点树上相邻
对于缩点树上相邻的两个连通块 ,一端在 、另一端在 的点对有 个。因此边权 的贡献为
最后把所有边权的贡献加起来即可
把原树以 为根。每条非根节点 都对应一条父边,记这条边的边权为
固定边权 ,把所有满足 的节点 看成切边根。对每个切边根 ,找到它最近的、边权同样为 的祖先 ;如果没有这样的祖先,就令
删除所有边权为 的边后:
- 切边根 对应的连通块,是 的子树去掉所有最近切边后代的子树
- 如果 ,父边 连接 对应的连通块和 对应的连通块
- 如果 ,这条父边连接 对应的连通块和根所在的连通块
设 是 的子树大小,那么 对应的连通块大小为
边权为 时,根所在的连通块大小为
所以,父边 对答案的贡献为
$$f(x) \begin{cases} f(p_x),&p_x\ne 0\\ f_c(0),&p_x=0 \end{cases} $$每条原树边都对应一个非根节点的父边,遍历所有非根节点累加贡献就可以得到答案
接下来只需要快速找到每个节点最近的同边权祖先
先对树做 DFS,记录每个点的 DFS 序
dfn和子树大小subsz。一个点 的子树在 DFS 序中是一段连续区间按 DFS 序遍历节点时,对每个边权维护一个祖先栈。处理非根节点 时:
- 查看边权 对应的栈顶
- 如果栈顶的子树区间不包含 ,说明它不是 的祖先,就弹出
- 弹到栈顶是 的祖先为止;此时栈顶就是最近的同边权祖先,栈空则不存在
- 把 放入对应的栈
每个节点最多进栈、出栈一次,因此找祖先是 的
整个过程都是 ,所以时间是 ,空间也是
-
-12
\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/

點分治輕鬆通過!
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 5e5 + 3; struct Edge { int x, c; }; int n, siz[N], dep[N], now[N]; ll ans, sum2[N], sum3[N], Total1, totalP; bool del[N]; vector<Edge> g[N]; vector<Edge> path; void S (int x, int fa, int col) { if (col) path.push_back ({x, col}); siz[x] = 1, dep[x] = dep[fa] + 1; for (auto to : g[x]) { if (to.x ^ fa && !del[to.x]) { S (to.x, x, to.c); siz[x] += siz[to.x]; } } } int Heavy (int x) { S (x, 0, 0); int p = x; for (auto i : path) { if (siz[i.x] > siz[x] / 2 && siz[i.x] < siz[p]) { p = i.x; } } path.clear (); return p; } ll single_cnt, single_sum2, single_sum3, all_sum2; void dfs1 (int x, int fa, int col) { if (col) { int c = col; if (now[c] == 0) { now[c] = 1; single_cnt++; single_sum2 += sum2[c]; single_sum3 += sum3[c]; all_sum2 += sum2[c]; } else if (now[c] == 1) { now[c] = 2; single_cnt--; single_sum2 -= sum2[c]; single_sum3 -= sum3[c]; } else { now[c]++; } } ans += single_cnt * totalP - single_sum2 - single_sum3 + Total1 - all_sum2; for (auto to : g[x]) { if (to.x ^ fa && !del[to.x]) { dfs1 (to.x, x, to.c); } } if (col) { int c = col; if (now[c] == 1) { now[c] = 0; single_cnt--; single_sum2 -= sum2[c]; single_sum3 -= sum3[c]; all_sum2 -= sum2[c]; } else if (now[c] == 2) { now[c] = 1; single_cnt++; single_sum2 += sum2[c]; single_sum3 += sum3[c]; } else { now[c]--; } } } void dfs2 (int x, int fa, int col) { ++now[col]; if (now[col] == 1) { sum2[col] += siz[x]; Total1 += siz[x]; } else if (now[col] == 2) { sum2[col] -= siz[x]; Total1 -= siz[x]; sum3[col] += siz[x]; } for (auto to : g[x]) { if (to.x ^ fa && !del[to.x]) { dfs2 (to.x, x, to.c); } } --now[col]; } void Calc (int x) { totalP = 1; Total1 = 0; for (auto to : g[x]) { if (!del[to.x]) { S (to.x, x, to.c); single_cnt = single_sum2 = single_sum3 = all_sum2 = 0; dfs1 (to.x, x, to.c); dfs2 (to.x, x, to.c); totalP += siz[to.x]; for (auto i : path) now[i.c] = 0; path.clear (); } } S (x, 0, 0); for (auto i : path) { sum2[i.c] = sum3[i.c] = 0; } path.clear (); totalP = Total1 = 0; } void Cute_LRH (int x) { int hvy = Heavy (x); Calc (hvy), del[hvy] = 1; for (auto to : g[hvy]) { if (!del[to.x]) { Cute_LRH (to.x); } } } int main () { ios :: sync_with_stdio (0), cin.tie (0); cin >> n; for (int i = 1, x, y, c; i < n; ++i) { cin >> x >> y >> c; g[x].push_back ({y, c}); g[y].push_back ({x, c}); } Cute_LRH (1); return cout << ans, 0 ^ 0; }
- 1
信息
- ID
- 75
- 时间
- 6000ms
- 内存
- 1000MiB
- 难度
- 5
- 标签
- 递交数
- 37
- 已通过
- 15
- 上传者






