Bestcoder round #65 && hdu 5593 ZYB ' s tree dp

Source: Internet
Author: User

Time limit:3000/1500 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 354 Accepted Submission (s): 100


Problem DescriptionZYB Have a tree withNNodes,now He wants you to solve the numbers of nodes distanced no more thanKFor each node.
The distance between and the nodes(x,y) is defined the number of edges on their shortest path in the tree.

To save the time of reading and printing,we use the following-to:

For Reading:we has a numbersAandB, letFAi Be the father of nodeI,Fa1=0 ,Fai=(a∗i+B)%(i−1)+1 Fori∈[2,N] .

For Printing:letansi Be the answer of nodeINeed to print thexor sum of all ansi.

Inputin The first line there is the number of testcasesT.

For each teatcase:

In the first line there is four numbersN,K,A,B

1≤T≤5 ,1≤N≤500000 , 1≤K≤,1≤A,B≤1000000

Outputfor T lines,each line print the ANS.

Please open the stack by yourself.

N≥100000 is only for the tests finally.

Sample Input13 1 1 1

Sample OUTPUT3

Sourcebestcoder Round #65 Test instructions: Given n nodes of a tree, for the first node, ans[i] is the tree in the distance from the node is less than the number of points of K, all the ans[i] different or up after the match, I feel that did not do it is very regrettable dp1[ I][J] Indicates the number of nodes in a tree with I as the distance from node I is exactly J, and after preprocessing, Dp1[i][j] becomes the number of the distance of node I as the root distance of I is less than or equal to J Dp2[i][j] Indicates that the distance from node I above (1 is root) is less than or equal to the number of J, after DP1, by transfer: dp2[v][j] = dp1[u][j-1]-dp1[v][j-2] + dp2[u][j-1) Drawing can be very well understood, note that the father of Node V is U, From V to U and V should correspond to j-2.
#include <bits/stdc++.h>using namespacestd;Const intINF =0x3f3f3f3f;Const intN =500005; typedefLong Longll;ll dp1[n][ A], dp2[n][ A];inthead[n], tot;intN, K, A, B;structedge{intu, V, next; Edge () {} Edge (intUintVintnext): U (U), V (v), next (next) {}}e[n];voidinit () {memset (head,-1,sizeofhead); memset (DP1,0,sizeofDP1); memset (DP2,0,sizeofDP2); Tot=0;}voidAddegde (intUintv) {E[tot]=Edge (U, V, head[u]); Head[u]= tot++;}voidDfsintu) {dp1[u][0] =1;  for(inti = Head[u]; ~i; i =E[i].next) {        intv =e[i].v;        DFS (v);  for(intj =1; J <= K; ++J) dp1[u][j] + = dp1[v][j-1]; }}voidDFS2 (intu) {     for(inti = Head[u]; ~i; i =E[i].next) {        intv =e[i].v; dp2[v][0] =0; dp2[v][1] =1;  for(intj =2; J <= K; ++j) Dp2[v][j]= Dp1[u][j-1]-Dp1[v][j-2] + dp2[u][j-1];    DFS2 (v); }}ll solve () {DFS (1);  for(inti =1; I <= N; ++i) for(intj =1; J <= K; ++j) Dp1[i][j]+ = Dp1[i][j-1]; DFS2 (1); ll ans=0;  for(inti =1; I <= N; ++i) ans ^= (dp1[i][k] +Dp2[i][k]); returnans;}intMain () {int_; scanf"%d", &_);  while(_ --) {scanf ("%d%d%d%d", &n, &k, &a, &c);        Init ();  for(inti =2; I <= N; ++i) {intF = (int) ((1LL * A * i + B)% (i-1) +1);        Addegde (f, i); } printf ("%lld\n", Solve ()); }    return 0;}

Bestcoder round #65 && hdu 5593 ZYB ' s 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.