标签:== 模板 span efi strong nbsp dfs family 深度
namespace tree { #define MAXN 100001 int sz[MAXN], dp[MAXN], fa[MAXN]; int hs[MAXN] = {0}; void dfs1(int x, int f, int dep) { dp[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 }
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; }
标签:== 模板 span efi strong nbsp dfs family 深度
原文地址:https://www.cnblogs.com/akakw1/p/9767893.html