POJ 1655-balancing Act (tree dp)

Source: Internet
Author: User

Test instructions

To find out which node in the tree of n nodes is removed, the maximum connectivity component is minimized.

Analysis: The previous question

#include <map>#include<Set>#include<list>#include<cmath>#include<queue>#include<stack>#include<cstdio>#include<vector>#include<string>#include<cctype>#include<complex>#include<cassert>#include<utility>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespaceStd;typedef pair<int,int>Pii;typedefLong Longll;#defineLson l,m,rt<<1#definePi ACOs (-1.0)#defineRson m+1,r,rt<<11#defineAll 1,n,1#defineN 20010#defineRead Freopen ("In.txt", "R", stdin)Constll infll =0x3f3f3f3f3f3f3f3fll;Const intinf=0x7ffffff;Const intMoD =1000000007;intDp[n],n,num[n],used[n];vector<int>E[n];intDfsintroot) {Num[root]=1; Used[root]=1;  for(intI=0; I<e[root].size (); + +i) {        intson=E[root][i]; if(Used[son])Continue; Num[root]+=Dfs (son); Dp[root]=Max (Dp[root],num[son]); } Dp[root]=max (dp[root],n-Num[root]); returnnum[root];}intMain () {intT; scanf ("%d",&t);  while(t--) {scanf ("%d",&N); Memset (DP,0,sizeof(DP)); memset (Used,0,sizeof(used));  for(intI=1; i<=n;++i) e[i].clear (); intb;  for(intI=0; i<n-1;++i) {scanf ("%d%d",&a,&b);            E[a].push_back (b);        E[b].push_back (a); } DFS (1); inttmp=1;  for(intI=2; i<n;++i)if(dp[tmp]>Dp[i]) tmp=i; printf ("%d%d\n", tmp,dp[tmp]); }return 0;}

POJ 1655-balancing Act (tree dp)

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.