其他分享
首页 > 其他分享> > 树

作者:互联网

树的重心

int sz[N],mins = 1e9,rt;
void dfs(int u,int fa) {
    sz[u] = 1; int maxs = 0;
    for(int i = head[u]; i;i = e[i].nxt) {
        int v = e[i].v; if(v == fa) continue;
        dfs(v,u);
        sz[u] += sz[v];
        if(maxs < sz[v]) maxs = sz[v];
    }
    maxs = max(maxs,n-sz[u]);
    if(maxs < mins) mins = maxs,rt = u;
}

标签:,sz,rt,int,maxs,dfs,mins
来源: https://www.cnblogs.com/mzg1805/p/11545225.html