POJ 1655:balancing Act

Source: Internet
Author: User

Balancing ACT Topic Links:

http://poj.org/problem?id=1655

Test instructions

Give a tree, seek the center of gravity of the tree and the number of nodes with the most nodes of the root node node, if there are more than the number of center of gravity output numbers smaller.

Exercises

The center of gravity of the tree: Find a node V, the root of V, so that V "node most sub-tree" node minimization

Water problem, casually with a node as the root of the deep search, where a node V "node most sub-tree" node number =min (sum[child[v]],n-∑sum[child[v]]-1)

      

Code
#include<stdio.h>Constint N=2e4+1;int cnt, head[N],ev[N*2],enext[N*2],res1, Res2;IntAbs(int x){return x>=0? x:-X;}voidAddedge(int U,Int V){Ev[CNT]=v; Enext[CNT]=head[u];head[u]=cnt++;}IntDfs(int ID,int FA,int n){int sum=0Mx=0;For(int I=head[ID];i!=-1; I=enext[I]){Int V=ev[I];If(V==fa)Continue;int W=Dfs(VIdN); sum+=w;If(W>mx) MX=w;}sum++;If(n-sum>mx) MX=n-sum;If(MX<res2|| (MX==res2&&id<res1)){Res2=mx; res1=id;}return sum;}voidClean(int n){CNT=0; res2=n+1;For(int I=1; I<=n; ++i) head[I]=-1;}voidSolve(){int TNXY;scanf("%d", &t);While(T--){scanf("%d", &n);If(n==1)While(1);Clean(n);For(int I=1; I<n; ++i){scanf("%d%d", &x, &y);Addedge(xY);Addedge(YX}dfs (1< Span class= "Sh_symbol" >,1,nprintf ( "%d%d\n< Span class= "sh_string" > ",res1,res2}}int main< Span class= "Sh_symbol" > () {solve (); Span class= "Sh_keyword" >return 0;}   

  

POJ 1655:balancing Act

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.