Helga Hufflepuff & #39; s Cup CodeForces,

Source: Internet
Author: User

Helga Hufflepuff's Cup CodeForces,

Helga Hufflepuff's Cup CodeForces-855C

To give a tree with n nodes, you must add value to each node. The added value can be an integer ranging from 1 to M. You must have at most x nodes with value-added k. If the added value of a node is k, the added value of the point directly connected to the node must be less than k. The total number of solutions that meet the requirements when adding value to the points of the entire tree.

Method:

Http://blog.csdn.net/ssimple_y/article/details/78081586

Ans [I] [j] [k] indicates that the maximum value of j is selected on the subtree with an I node as the root and k meets the condition (k = 0 indicates that I select a value smaller than k, k = 1 indicates the number of methods when I select a value of k and k = 2 indicates that I select a value greater than k. Obviously, you can start dp with any node as the root.

For each node, a small dp is used to calculate the result.

T [I] [j] [p] indicates the number of solutions where j (p is 0, 1, or 2) are selected for the first I subnode (current node) under p. Set the subnode I to xx.

Before starting, because even a subnode is not considered, k =, 2 also have K-1, 1, m-k methods, that is, the number of sub-nodes are also multiplied by the K-1, 1, m-k, to first assigned to the t array.

$ T [I] [j] [0] = sum \ {t [I-1] [j-y] [0] * (ans [xx] [y] [0] + ans [xx] [y] [1] + ans [xx] [y] [2]) \} $

$ T [I] [j] [1] = sum \ {t [I-1] [j-y] [1] * (ans [xx] [y] [0]) \} $

$ T [I] [j] [2] = sum \ {t [I-1] [j-y] [2] * (ans [xx] [y] [0] + ans [xx] [y] [2]) \} $

Finally, for each node u, its ans [u] [j] [k] is equal to the number of t [u subnodes] [j] [k].

Of course, you can use the scrolling array to optimize t.

1 # include <cstdio> 2 # include <cstring> 3 # define md 1000000007 4 typedef long LL; 5 struct Edge 6 {7 LL to, next; 8} edge [200100]; 9 LL k, m, n, x, anss; 10 LL f1 [100100], n_e; 11 LL ans [100100] [11] [3]; 12 bool vis [100100]; 13 void m_e (LL a, LL B) 14 {15 edge [++ n_e]. to = B; 16 edge [n_e]. next = f1 [a]; 17 f1 [a] = n_e; 18 edge [++ n_e]. to = a; 19 edge [n_e]. next = f1 [B]; 20 f1 [B] = n_e; 21} 22 void dfs (LL u) 23 {24 vis [u] = true; 2 5 LL kk, xx, j, q, ii = 0; 26 LL t [2] [11] [3]; // The Rolling array 27 memset (t [0], 0, sizeof (t [0]); 28 t [0] [0] [0] = K-1; 29 t [0] [1] [1] = 1; 30 t [0] [0] [2] = m-k; 31 for (kk = f1 [u]; kk! = 0; kk = edge [kk]. next) 32 {33 if (! Vis [edge [kk]. to]) 34 {35 ii ^ = 1; 36 memset (t [ii], 0, sizeof (t [ii]); 37 xx = edge [kk]. to; 38 dfs (xx); 39 memset (t [ii], 0, sizeof (t [ii]); 40 for (j = 0; j <= x; j ++) 41 for (q = 0; q <= x; q ++) 42 {43 if (j <q) break; 44 t [ii] [j] [0] = (t [ii] [j] [0] + t [ii ^ 1] [j-q] [0] * (ans [xx] [q] [0] + ans [xx] [q] [1] + ans [xx] [q] [2]) % md; 45 t [ii] [j] [1] = (t [ii] [j] [1] + t [ii ^ 1] [j-q] [1] * ans [xx] [q] [0]) % md; 46 t [ii] [j] [2] = (t [ii] [j] [2] + t [ii ^ 1] [j-q] [2] * (ans [xx] [q] [0] + ans [xx] [q] [2]) % md; 47} 48} 49} 50 memcpy (ans [u], t [ii], sizeof (ans [u]); 51} 52 int main () 53 {54 LL a, B, I, j; 55 scanf ("% I64d % I64d", & n, & m); 56 for (I = 1; I <n; I ++) 57 {58 scanf ("% I64d % I64d", & a, & B); 59 m_e (a, B ); 60} 61 scanf ("% I64d % I64d", & k, & x); 62 dfs (1); 63 for (I = 0; I <= x; I ++) 64 for (j = 0; j <3; j ++) 65 anss = (anss + ans [1] [I] [j]) % md; 66 printf ("% I64d", anss); 67 return 0; 68}

In actual implementation, you can assign all the tvalues of a subnode to ans [u] Every time you compute them. When you compute the next node, for example, to access t [I-1] [j-y] [2], it is equivalent to the ans [u] [j-y] [2] of this method. This avoids opening a t array for each subnode.

 1 #include<cstdio> 2 #include<cstring> 3 #define md 1000000007 4 typedef long long LL; 5 struct Edge 6 { 7     LL to,next; 8 }edge[200100]; 9 LL k,m,n,x,anss;10 LL f1[100100],n_e;11 LL ans[100100][11][3];12 LL t[11][3];13 bool vis[100100];14 void m_e(LL a,LL b)15 {16     edge[++n_e].to=b;17     edge[n_e].next=f1[a];18     f1[a]=n_e;19     edge[++n_e].to=a;20     edge[n_e].next=f1[b];21     f1[b]=n_e;22 }23 void dfs(LL u)24 {25     vis[u]=true;26     LL kk,xx,j,q;27     ans[u][0][0]=k-1;28     ans[u][1][1]=1;29     ans[u][0][2]=m-k;30     for(kk=f1[u];kk!=0;kk=edge[kk].next)31     {32         if(!vis[edge[kk].to])33         {34             xx=edge[kk].to;35             dfs(xx);36             memset(t,0,sizeof(t));37             for(j=0;j<=x;j++)38                 for(q=0;q<=x;q++)39                 {40                     if(j<q)    break;41                     t[j][0]=(t[j][0]+ans[u][j-q][0]*(ans[xx][q][0]+ans[xx][q][1]+ans[xx][q][2]))%md;42                     t[j][1]=(t[j][1]+ans[u][j-q][1]*ans[xx][q][0])%md;43                     t[j][2]=(t[j][2]+ans[u][j-q][2]*(ans[xx][q][0]+ans[xx][q][2]))%md;44                 }45             for(j=0;j<=x;j++)46                 for(q=0;q<3;q++)47                     ans[u][j][q]=t[j][q];48         }49     }50 }51 int main()52 {53     LL a,b,i,j;54     scanf("%I64d%I64d",&n,&m);55     for(i=1;i<n;i++)56     {57         scanf("%I64d%I64d",&a,&b);58         m_e(a,b);59     }60     scanf("%I64d%I64d",&k,&x);61     dfs(1);62     for(i=0;i<=x;i++)63         for(j=0;j<3;j++)64             anss=(anss+ans[1][i][j])%md;65     printf("%I64d",anss);66     return 0;67 }

Error records:

(Second code)

I forgot to write 40 rows, causing the array to access WA out of bounds.

I used to follow the first code, but did not open a t array for each dfs, resulting in WA.

Official question:

Http://codeforces.com/blog/entry/54750

Http://codeforces.com/blog/entry/54750? # Comment-387718

This problem can be solved using precomputation of dp tableDp[Base] [Mask] [Len]. This stores the number of integers in baseBAnd lengthLenThat forms the givenMaskIn their representation.MaskIs defined as havingIAccept-Encoding-ThBit as 1, if the digitIAudio-extract 1 occurs odd number of times in the representation.

Using this precomputedDpArray, we can easily calculate the answer for the queries, by convertingLBetween-between 1 andRTo the given baseB, Then adding the total integers less than equalRWithMaskRows = lower 0 and subtracting those lessLWithMaskBytes = bytes 0.

Now, to find the number of integers less than equalLStarts-starts 1MaskKeys = defaults 0, we first add all the integersMaskRows = Limit 0 who have length less than lengthLSkip-commit 1 in baseBRepresentation. If lengthLSkip-commit 1 in baseBIsLB, This value can be calculated as. The second term is subtracted to take into account the trailing zeros.

Now, we need to calculate the number of integers with length limit = percentLBAnd value limit ≤ limitLBetween-between 1 andMaskLimit = Limit 0. Let the numberLSkip-commit 1 in baseBRepresentation beL0, average,L1...LLB. Then, if we fix the first digit of our answer,XFrom 0L0 blocks-keys 1, we can simply calculate the mask for remaining digits we need as 2XAnd thus addingDp[B] [2X] [LenAccept-limit 1] to answer.

Now, if we fix the first digitL0 only, we can simply perform the same operation for the second digit, selecting value of second digit,YFrom 0L1 rows-between 1, and thus adding to answer. And, we can move forward to rest of the digits in the same way.

The overall complexity of the solution will be

Let's say we want to calculateDp[V] [J] [X] (Means the number of ways of gettingxNumberkType nodes in the subtree rooted at v, where type (v) = j) how to calculate this-let's assumeF(V, Bytes,J, Bytes,X) Has the same definitionDp[V] [J] [X].

Say we havenChildren of node v. so essential what we need to find is the number of ways to distributexAmong these n children.

Here we can use a dp. (for convenience I'll call nodes of typekAs special node) Now, to do this computation at node v, we will form another DPDp1. We say as the number of ways to choose a total of x special nodes from subtrees definedV1. zookeeper,V2, please wait..., please wait ,...,VII. e. from firstiNodes. The recurrence can be defined as, I. e. we are iterating over y assuming that subtreeVIContributes y special nodes and rest x-y special nodes have been contributed by previous I-1 nodes. So, finallyDp[V] [J] [X] Bytes = bytesDp1 (N, Bytes,J, Bytes,X)

In the editorial solution thisDp1 is denotedAAndBArray. you wont findIIn the editorial'sDp1 state,ICan be avoided by using two arraysAAndB. We storeDp1 (I, Threads, threads) inBArray, and after its calculation it is addedAArray, so this will becomeDp1 (ILower-limit 1, lower, lower) for the next iteration.

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.