版本比较
树上启发式合并
正文
增加 9 行删除 9 行
84 行未改动85868788899091929337 行未改动13113213313413513613713813914014114214314414514614714814915015115284 行未改动continue;dfs(v, u);siz[u] += siz[v];if (siz[v] > siz[son[u]])son[u] = v;}}int maxi;37 行未改动cin >> c[i];for (int i = 1, x, y; i < n; i++) {cin >> x >> y;e[x].push_back(y);e[y].push_back(x);}dfs(1, 0);solve(1, 0);for (int i = 1; i <= n; i++)cout << ans[i] << ' ';return 0;}```树上启发式合并用起来还是比较板的,大部分情况下只需要考虑怎么改 update 的前半部分就行了,dfs、clear、solve 看情况稍微变一下就行了。其他例题:[CF375D Tree and Queries(普通 DSU on Tree)](https://www.luogu.com.cn/problem/CF375D);[CF246E Blood Cousins Return(与深度有关的 DSU on Tree)](https://www.luogu.com.cn/problem/CF246E);[CF1009F Dominant Indices(与深度有关的 DSU on Tree)](https://www.luogu.com.cn/problem/CF1009F);[CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(与路径有关的 DSU on Tree)](https://www.luogu.com.cn/problem/CF741D);[CF914E Palindromes in a Tree(与路径有关的 DSU on Tree)](https://www.luogu.com.cn/problem/CF914E);[AT_abc359_g [ABC359G] Sum of Tree Distance(与路径有关的 DSU on Tree)](https://www.luogu.com.cn/problem/AT_abc359_g);[P4103 [HEOI2014] 大工程(与路径有关的 DSU on Tree)](https://www.luogu.com.cn/problem/P4103)。84 行未改动85868788899091929337 行未改动13113213313413513613713813914014114214314414514614714814915015115284 行未改动continue;dfs(v, u);siz[u] += siz[v];if (siz[v] > siz[son[u]])son[u] = v;}}int maxi;37 行未改动cin >> c[i];for (int i = 1, x, y; i < n; i++) {cin >> x >> y;e[x].push_back(y);e[y].push_back(x);}dfs(1, 0);solve(1, 0);for (int i = 1; i <= n; i++)cout << ans[i] << ' ';return 0;}```树上启发式合并用起来还是比较板的,大部分情况下只需要考虑怎么改 update 的前半部分就行了,dfs、clear、solve 看情况稍微变一下就行了。其他例题:[CF375D Tree and Queries(普通 DSU on Tree)](https://www.luogu.com.cn/problem/CF375D);[CF246E Blood Cousins Return(与深度有关的 DSU on Tree)](https://www.luogu.com.cn/problem/CF246E);[CF1009F Dominant Indices(与深度有关的 DSU on Tree)](https://www.luogu.com.cn/problem/CF1009F);[CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(与路径有关的 DSU on Tree)](https://www.luogu.com.cn/problem/CF741D);[CF914E Palindromes in a Tree(与路径有关的 DSU on Tree)](https://www.luogu.com.cn/problem/CF914E);[AT_abc359_g [ABC359G] Sum of Tree Distance(与路径有关的 DSU on Tree)](https://www.luogu.com.cn/problem/AT_abc359_g);[P4103 [HEOI2014] 大工程(与路径有关的 DSU on Tree)](https://www.luogu.com.cn/problem/P4103)。