How to use:
- sz (size) storage subtree size
- DP (deep) storage node depth
- fa (father) storage node Father
- HS (heavy son) storage node heavy son
- HF stores the top node of the current node's chain
- ID Storage node number
- nd (node) stores the current number Corresponding node
Namespace Tree {#define MAXN 100001int SZ[MAXN], DP[MAXN], fa[maxn];int hs[maxn] = {0};void DFS1 (int x, int f, int dep) {D P[X] = DEP, fa[x] = f, sz[x] = 1;int Hsz = 0;for (int i = head[x]; i; i = Nxt[i]) {if (ver[i] = = f) continue;dfs1 (Ver[i], X, DEP + 1); Sz[x] + = sz[ver[i]];if (Sz[ver[i] > Hsz) {hsz = sz[ver[i]];hs[x] = Ver[i];}}} int HF[MAXN], ID[MAXN], nd[maxn];int cnt = 0;void DFS2 (int x, int HFA) {Hf[x] = HFA, id[x] = + + cnt, nd[cnt] = x;if (Hs[x]) DFS2 (Hs[x], HFA); for (int i = head[x]; i; i = Nxt[i]) if (ver[i]! = hs[x] && ver[i]! = fa[x]) dfs2 (Ver[i], ver[i]);} #undef MAXN}
Storage method: Chain-forward star
int head[100001] = {0}, nxt[200001], ver[200001], tot = 0;void Add (int x, int y) { ver[++ tot] = y, Nxt[tot] = head[x] , head[x] = tot; ver[++ tot] = x, Nxt[tot] = Head[y], head[y] = tot;}
Template tree Chain split