zoukankan      html  css  js  c++  java
  • NC15033 小G有一个大树

    树的重心模板。

    自底向上进行统计以每个结点u为根的子树大小。

    对于一个节点u,如果我们把它从树中删除,那么原来的一棵树可能会分成若干个不相连的部分,其中每一部分都是一颗子树。

    设maxpart表示在删除节点u之后产生的子树中,最大的一颗的大小。使maxpart取到最小值的节点就是树的重心。

    const int N=1e5+10;
    vector<int> g[N];
    int n;
    int ans=N;
    int pos;
    
    int dfs(int u,int fa)
    {
        int tot=1; // 以u为根的子树大小
        int maxpart=0;
        for(int i=0;i<g[u].size();i++)
        {
            int j=g[u][i];
            if(j == fa) continue;
            int cnt=dfs(j,u);
            maxpart=max(maxpart,cnt);
            tot+=cnt;
        }
    
        maxpart=max(maxpart,n-tot);
        if(maxpart< ans)
        {
            ans=maxpart;
            pos=u;
        }
        else if(maxpart== ans && u < pos)
            pos=u;
        
        return tot;
    }
    
    int main()
    {
        cin>>n;
    
        for(int i=0;i<n-1;i++)
        {
            int a,b;
            cin>>a>>b;
            g[a].pb(b);
            g[b].pb(a);
        }
    
        dfs(1,-1);
        
        cout<<pos<<' '<<ans<<endl;
        //system("pause");
        return 0;
    }
    
  • 相关阅读:
    form 编译命令
    Form文件夹开发步骤
    使用View为Data Source的Form开发要点
    spring2.0包说明【转】
    Zero to One读后感
    Fourth glance in Go
    Third glance in Go
    Second glance in Go
    First glance in Go
    MongoDB 安装
  • 原文地址:https://www.cnblogs.com/fxh0707/p/14634277.html
Copyright © 2011-2022 走看看