POJ 3107 Godfather (center of gravity of the tree)

Source: Internet
Author: User

POJ 3107 Godfather (center of gravity of the tree)

 
Or the center of gravity of the naked tree, but this requirement outputs all the centers of gravity. Very simple.
The Code is as follows:

#include 
  
   #include 
   
    #include 
    
     #include 
     
      #include #include 
      
       #include
       #include 
        
         #include 
         
          #include 
          
           using namespace std;#define LL __int64#define pi acos(-1.0)//#pragma comment(linker, /STACK:1024000000)#define root 1, n, 1#define lson l, mid, rt<<1#define rson mid+1, r, rt<<1|1const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-9;const int MAXN=50000+10;struct node { int v, next;} edge[MAXN<<1];int head[MAXN], cnt, min1, n, num;int sum[MAXN], ans[MAXN];void add(int u, int v){ edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++;}void init(){ memset(head,-1,sizeof(head)); cnt=0; min1=INF;}void dfs(int u, int fa){ int max1=0, i, tot=0; sum[u]=1; for(i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(v==fa) continue ; dfs(v,u); sum[u]+=sum[v]; max1=max(max1,sum[v]); tot+=sum[v]; } max1=max(max1,n-tot-1); if(min1>max1) { min1=max1; num=0; ans[num++]=u; } else if(min1==max1) { ans[num++]=u; }}int main(){ int u, v, i; while(scanf(%d,&n)!=EOF) { init(); for(i=1; i
           
          
         
        
      
     
    
   
  

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.