[HAOI2015] Tree dyed (tree backpack)

Source: Internet
Author: User

There is a tree with a number of N, and the edge of the tree is right. Give you a positive integer k within 0~ N, you want to select K points in this tree, dye it black, and turn the other n-k Dianran into white. After all points are dyed, you will get the distance between the black dots 22 plus the white Point 22 distance and the benefit. Ask what the maximum benefit is.

Solution

Compare the classic tree knapsack problem.

If only the points are analyzed, the situation becomes very cumbersome and does not take into account each variable contribution, each edge produces a product of the number of black dots on both sides plus the product of the white dots on both sides.

In this way we can run the backpack directly, the standard tree-shaped backpack is n^3, but this problem each word backpack volume has an upper limit, the total complexity can be done n^2.

Code

#include <iostream>#include<cstdio>#include<cstring>#defineN 2009using namespacestd;Long LongDp[n][n];intSize[n],m,n,a,b,c,tot,head[n];structdsd{intn,to,l;} An[n<<1];inlinevoidAddintUintVintl) {an[++tot].n=Head[u]; An[tot].to=v; Head[u]=tot; AN[TOT].L=l;}voidDfsintUintFA) {Size[u]=1;  for(intI=head[u];i;i=AN[I].N)if(an[i].to!=FA) {      intv=an[i].to;    DFS (V,U); Size[u]+=Size[v];  for(intJ=min (M,size[u]); j>=0;--j)//       for(intk=0; K<=min (J,size[v]); + +k)if(dp[v][k]!=-0x3f3f3f3f)      {          Long LongNum= (Long Long) (k* (m-k) + (n-size[v]-(m-k)) * (SIZE[V]-K)) *AN[I].L; DP[U][J]=max (Dp[u][j],dp[u][j-k]+dp[v][k]+num); }  } }intMain () {scanf ("%d%d",&n,&m);  for(intI=1; i<n;++i) {scanf ("%d%d%d",&a,&b,&B);    Add (a,b,c); add (b,a,c); } memset (DP,-0x3f,sizeof(DP));  for(intI=1; i<=n;++i) dp[i][0]=dp[i][1]=0;//Dfs1,0); cout<<dp[1][m]; return 0;} 

This writing is too slow, and did not do strictly N^2,bzoj will tle, the following writing is stable.

Code

#include <iostream>#include<cstdio>#include<cstring>#defineN 2009using namespaceStd;typedefLong LongLl;ll Dp[n][n],size[n],m,n,a,b,c,tot,head[n],g[n];structdsd{ll N,to,l;} An[n<<1];inlinevoidAdd (ll u,ll v,ll l) {an[++tot].n=Head[u]; An[tot].to=v; Head[u]=tot; AN[TOT].L=l;} ll mi (ll X,ll y) {returnX<y?x:y;} ll Ma (ll x,ll y) {returnX<y?y:x;}voidDfs (ll u,ll FA) {Size[u]=1;  for(LL i=head[u];i;i=AN[I].N)if(an[i].to!=FA) {LL v=an[i].to;    DFS (V,U); ll x=mi (M,size[u]), y=mi (m,size[v]);  for(intj=0; j<=m;++j) g[j]=0;  for(LL j=x;j>=0;--j) for(intk=0; k<=y;++k)if(j+k<=m) {ll Gyx= (ll) k* (m-k) + (n-size[v]-(m-k)) * (SIZE[V]-K)) *AN[I].L; G[j+k]=ma (g[j+k],dp[u][j]+dp[v][k]+Gyx); }     for(intj=0; j<=m;++j) dp[u][j]=G[j]; Size[u]+=Size[v]; }}inlineintRd () {intx=0;CharC=GetChar ();  while(!isdigit (c)) c=GetChar ();  while(IsDigit (c)) {x= (x<<1) + (x<<3) + (c^ -); C=GetChar (); }    returnx;} intMain () {n=rd (); m=Rd ();  for(intI=1; i<n;++i) {a=rd (); B=rd (); c=Rd ();    Add (a,b,c); add (b,a,c); } memset (DP,-0x3f,sizeof(DP));  for(intI=1; i<=n;++i) dp[i][0]=dp[i][1]=0; DFS (1,0); printf ("%lld", dp[1][m]); return 0;} 

[HAOI2015] Tree dyed (tree backpack)

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.