Tree DP calculates the center of gravity of the tree-sgu 134

Source: Internet
Author: User

The attribute value of a vertex is: the maximum number of nodes of the connected component obtained after removing the vertex and all the edges connected to the vertex.

The center of gravity of the tree is defined as: a vertex. The attribute value of this vertex is the smallest among all vertices.

Sgu 134 identifies all centers of gravity and the attribute values of the center of gravity.

Consider using a tree-like DP.

DP [u] indicates the maximum number of nodes in the connected branch after the U point is cut.

TOT [u] records the total number of nodes (including the root) of the subtree rooted in U ).

Then, the two Arrays can be preprocessed with a DFS. Then enumerate each vertex. The attribute value of each vertex is actually max (DP [u], n-tot [u]), because it is possible that the largest connected branch is at or above U's father. Then record the answer.

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#include <vector>using namespace std;vector<int> G[16005];int dp[16005],tot[16005];vector<int> ans;void dfs(int u){    tot[u] = 1;    for(int i=0;i<G[u].size();i++)    {        int v = G[u][i];        if(tot[v] == -1)            dfs(v);        else            continue;        dp[u] = max(dp[u],tot[v]);        tot[u] += tot[v];    }}int main(){    int n,i,j,u,v;    scanf("%d",&n);    for(i=0;i<=n;i++)        G[i].clear();    for(i=0;i<n-1;i++)    {        scanf("%d%d",&u,&v);        G[u].push_back(v);        G[v].push_back(u);    }    memset(dp,0,sizeof(dp));    memset(tot,-1,sizeof(tot));    dfs(1);    int mini = Mod;    for(i=1;i<=n;i++)    {        int tmp = max(dp[i],n-tot[i]);        if(tmp < mini)        {            ans.clear();            ans.push_back(i);            mini = tmp;        }        else if(tmp == mini)            ans.push_back(i);    }    printf("%d %d\n",mini,ans.size());    sort(ans.begin(),ans.end());    printf("%d",ans[0]);    for(i=1;i<ans.size();i++)        printf(" %d",ans[i]);    puts("");    return 0;}
View code

 

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.