bzoj1063: [Noi2008] Road design

Source: Internet
Author: User

Tree-shaped DP.

This problem is divided into the same way as the tree chain, but the heavy path can pass through the root node, and requires the number of schemes, so can not be used to split the tree chain.

But by this relationship you can tell that the answer is very small for the log n level (after looking at the data, the answer is the maximum of 4). But there should be more data to make the answer bigger.

With F[I][J][0/1/2], respectively, in the node with I as a subtree, the point of the most convenient value is J.

0 means that the I node is not in the chain of any one son, 1 means that in a chain composed of a son, 2 represents a chain of 2 sons that passes through the I node.

(This is a hard to understand feeling.) )

A recursive relationship between them ... Then I won't be typing under this box, as explained above.

F1 calculates the number of scenarios that v[i] can link to the root node. F2 represents the number of scenarios that are not connected to the root node.

 f1=f[v[i]][j][0 ]+f[v[i]][j][1  Span style= "COLOR: #000000" >]f2  = (j==0 ?  0 :(F[v[i]][j-1 ][0 ]+f[v[i]][j-< Span style= "COLOR: #800080" >1 ][1 ]+f[v[i]][j- 1 ] [2   2 ]=f[u][j][1 ]*f1+f[u][j ][2 ]*f2;f[u][j][ 1 ] =f[u][j][0 ]*f1+f[u][j][1 ]*f2;f[u][j][ 0 ]=f[u][j][0 ]*f2; 
#include <cstdio>#include<algorithm>#include<cstring>#defineLL Long Longusing namespacestd;Const intMAXN =200000+Ten;Const intMaxh = A;intG[maxn],v[maxn],next[maxn],eid;Long Longf1,f2,f[maxn][maxh][3],res,mod;intN,m,ans;voidAddedge (intAintb) {V[eid]=b; Next[eid]=g[a]; g[a]=eid++; V[eid]=a; NEXT[EID]=G[B]; g[b]=eid++;} ll update (ll x) {if(X%mod)returnX%MoD; returnx==0?0: mod; }voidDfsintUintFA) {     for(intI=g[u];~i;i=next[i])if(v[i]!=FA)        {DFS (V[I],U);  for(intj=0; j<maxh;j++) {F1=update (f[v[i]][j][0]+f[v[i]][j][1]); F2= (j==0?0: Update (f[v[i]][j-1][0]+f[v[i]][j-1][1]+f[v[i]][j-1][2])); f[u][j][2]=update (f[u][j][1]*f1+f[u][j][2]*F2); f[u][j][1]=update (f[u][j][0]*f1+f[u][j][1]*F2); f[u][j][0]=update (f[u][j][0]*F2); }    }}intMain () {memset (g,-1,sizeof(g)); scanf ("%d%d%lld",&n,&m,&MoD); if(m!=n-1) {printf ("-1\n-1\n"); return 0; }     for(intI=1, u,v;i<n;i++) {scanf ("%d%d",&u,&v);    Addedge (U,V); }     for(intI=1; i<=n;i++)     for(intj=0; j<maxh;j++) f[i][j][0]=1; DFS (1,0);  for(intI=0, res;i<maxh;i++) {res=update (f[1][i][0]+f[1][i][1]+f[1][i][2]); if(res) {ans=res%MoD; printf ("%d\n%d\n", I,ans);  Break; }    }    return 0;}

bzoj1063: [Noi2008] Road design

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.