Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 354    Accepted Submission(s): 100

Problem Description
ZYB has a tree with N nodes,now he wants you to solve the numbers of nodes distanced no more than K for each node.
the distance between two 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 way:

For reading:we have two numbers A and B,let fai be the father of node i,fa1=0,fai=(A∗i+B)%(i−1)+1 for i∈[2,N] .

For printing:let ansi be the answer of node i,you only need to print the xor sum of all ansi.

 
Input
In the first line there is the number of testcases T.

For each teatcase:

In the first line there are four numbers N,K,A,B

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

 
Output
For T lines,each line print the ans.

Please open the stack by yourself.

N≥100000 are only for two tests finally.

 
Sample Input
1
3 1 1 1
 
Sample Output
3
 
Source
 
题意:给出n个节点的一棵树,对于第i个节点,ans[i]是树中离该节点的距离小于等于k的点的个数,把所有的ans[i]异或起来
赛后补的了,觉得当时没做出来也是很遗憾了
dp1[i][j] 表示以i为根的树中距离节点i距离恰好为j的节点个数,预处理之后再推一下,dp1[i][j]就变成以i为根距离节点i的距离小于等于j的个数
dp2[i][j] 表示节点i的上方(1为根)距离节点i的距离小于等于j的个数,有了dp1后,通过转移:
dp2[v][j] = dp1[u][j-1] - dp1[v][j-2] + dp2[u][j-1]
画图就能很好理解,注意的是节点v的父亲是u,从v到u再到v应该对应j-2了
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int INF = 0x3f3f3f3f;
  4. const int N = ;
  5. typedef long long ll;
  6. ll dp1[N][], dp2[N][];
  7. int head[N], tot;
  8. int n, k, A, B;
  9. struct Edge{
  10. int u, v, next;
  11. Edge() {}
  12. Edge(int u, int v, int next) : u(u), v(v), next(next) {}
  13. }e[N];
  14. void init() {
  15. memset(head, -, sizeof head);
  16. memset(dp1, , sizeof dp1);
  17. memset(dp2, , sizeof dp2);
  18. tot = ;
  19. }
  20. void addegde(int u, int v) {
  21. e[tot] = Edge(u, v, head[u]);
  22. head[u] = tot++;
  23. }
  24. void dfs(int u)
  25. {
  26. dp1[u][] = ;
  27. for(int i = head[u]; ~i; i = e[i].next) {
  28. int v = e[i].v;
  29. dfs(v);
  30. for(int j = ; j <= k; ++j) dp1[u][j] += dp1[v][j - ];
  31. }
  32. }
  33. void dfs2(int u)
  34. {
  35. for(int i = head[u]; ~i; i = e[i].next) {
  36. int v = e[i].v;
  37. dp2[v][] = ; dp2[v][] = ;
  38. for(int j = ; j <= k; ++j)
  39. dp2[v][j] = dp1[u][j - ] - dp1[v][j - ] + dp2[u][j - ];
  40. dfs2(v);
  41. }
  42. }
  43. ll solve()
  44. {
  45. dfs();
  46. for(int i = ; i <= n; ++i)
  47. for(int j = ; j <= k; ++j)
  48. dp1[i][j] += dp1[i][j - ];
  49. dfs2();
  50. ll ans = ;
  51. for(int i = ; i <= n; ++i) ans ^= (dp1[i][k] + dp2[i][k]);
  52. return ans;
  53. }
  54. int main()
  55. {
  56. int _; scanf("%d", &_);
  57. while(_ --)
  58. {
  59. scanf("%d%d%d%d", &n, &k, &A, &B);
  60. init();
  61. for(int i = ; i <= n; ++i) {
  62. int f = (int)((1ll * A * i + B) % (i - ) + );
  63. addegde(f, i);
  64. }
  65. printf("%lld\n", solve());
  66. }
  67. return ;
  68. }

Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp的更多相关文章

  1. HDU 5593 ZYB's Tree 树形dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5593 题意: http://bestcoder.hdu.edu.cn/contests/contes ...

  2. Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  3. HDU5593 ZYB's Tree 树形DP +分治

    感觉其实就是树分治,一次BC的题,感觉这次题目质量比较高,仅代表蒟蒻的看法 一次DFS获取每个点到子树的距离不大于K的点的个数, 然后一遍BFS获取从每个点父亲不大于K的的个数,层层扩展,还是想说 其 ...

  4. hdu5593/ZYB's Tree 树形dp

    ZYB's Tree    Memory Limit: 131072/131072 K (Java/Others) 问题描述 ZYBZYB有一颗NN个节点的树,现在他希望你对于每一个点,求出离每个点距 ...

  5. codeforces Round #263(div2) D. Appleman and Tree 树形dp

    题意: 给出一棵树,每个节点都被标记了黑或白色,要求把这棵树的其中k条变切换,划分成k+1棵子树,每颗子树必须有1个黑色节点,求有多少种划分方法. 题解: 树形dp dp[x][0]表示是以x为根的树 ...

  6. HUD 5593——ZYB's Tree——————【树形dp】

    ZYB's Tree Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tota ...

  7. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  8. 熟练剖分(tree) 树形DP

    熟练剖分(tree) 树形DP 题目描述 题目传送门 分析 我们设\(f[i][j]\)为以\(i\)为根节点的子树中最坏时间复杂度小于等于\(j\)的概率 设\(g[i][j]\)为当前扫到的以\( ...

  9. BestCoder Round #65

    博弈 1002 ZYB's Game 题意:中文 分析:假定两个人是绝顶聪明的,一定会采取最优的策略.所以如果选择X的左边的一个点,那么后手应该选择X的右边对称的点,如果没有则必输,否则必胜,然后再分 ...

随机推荐

  1. iOS block 的底层实现

    其实swift 的闭包跟 OC的block 是一样一样的,学会了block,你swift里边的闭包就会无师自通. 参考:http://www.jianshu.com/p/e23078c11518 ht ...

  2. iOS注册collcetionViewFlowLayout

    self.arr = [[NSMutableArray alloc] init]; for (int i = 0; i < 9; i++) { [self.arr addObject:[UIIm ...

  3. UVa1593_Allgnment_Of_Code

    /** start: integer; // begins hear stop: integer; // ends here s: string; c: char; // temp **/ //测试数 ...

  4. java 学习笔记——网络(Socket)

    阅读方法:将网页放大到200%. 如果你用过用过word应该知道按住ctrl键使用鼠标滚轮缩放.

  5. postgresql导入及导出

    导入命令 psql -d GAME -U postgres -f /root/plubic.sql 出现如下错误: psql: FATAL:  Peer authentication failed f ...

  6. 有关struts2中用到 js 总结

    1.js中取Struts2中的栈里的值 var current = "${currentPage}"; 2.js 如何提交执行提交url连接 ,以及 Struts中的url如何如何 ...

  7. Lattice 的 Framebuffer IP核使用调试笔记之IP核生成与参数设置

    本文由远航路上ing 原创,转载请标明出处. 这节笔记记录IP核的生成以及参数设置. 先再IP库里下载安装Framebuffer 的ipcore 并安装完毕. 一.IP核的生成: 1.先点击IP核则右 ...

  8. android HTTPclient

    Apache包是对android联网访问封装的很好的一个包,也是android访问网络最常用的类. 下面分别讲一下怎么用HttpClient实现get,post请求. 1.Get 请求 1 2 3 4 ...

  9. php 分页

    分页类 <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private ...

  10. Validform 学习笔记---代码练习

    上一节主要梳理了validform的基础知识,针对这些基础知识,编写代码的时候,也整理的部分知识,先记录以便后期温习. 验证部分的css @charset "utf-8"; /* ...