"Template" Tree chain split

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.