POJ 1655 Balancing Act (center of gravity of the tree)

Source: Internet
Author: User

Seek the center of gravity of the tree, directly as a template bar. First look at the POJ topic to know the center of gravity what meaning ...

Center of Gravity: Minimum number of nodes for maximum connected blocks after deleting this node

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <queue>5#include <stack>6 using namespacestd;7 #defineLL Long Long8 #defineCLC (A, B) memset (A,b,sizeof (a))9 #defineINF 0x3f3f3f3fTen Const intmaxn=20010; Onevector<int>V[MAXN]; A intVIS[MAXN],DP[MAXN],SON[MAXN]; - intAns,sizee,n; - voidDfsintx) { the     intBal=0; -son[x]=0; -vis[x]=true; -      for(intI=0;i< (int) v[x].size (); i++){ +         intto=V[x][i]; -         if(Vis[to])Continue; + Dfs (to); Ason[x]+=son[to]+1; atBal=max (bal,son[to]+1); -     } -Bal=max (bal,n-son[x]-1); -     if(bal<sizee| | (Bal==sizee && x<ans)) { -Ans=x,sizee=Bal; -     } in } - intMain () { to     intT; +scanf"%d",&T); -      while(t--){ thescanf"%d",&n); *      for(intI=1; i<=n;i++) $ v[i].clear ();Panax Notoginseng      for(intI=1; i<=n-1; i++){ -         intx, y; thescanf"%d%d",&x,&y); + v[x].push_back (y); A v[y].push_back (x); the     } +CLC (VIS,0); -Sizee=inf; $Dfs1); $printf"%d%d\n", Ans,sizee); -     } -     return 0; the}
View Code

POJ 1655 Balancing Act (center of gravity of the tree)

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.