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了
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = ;
typedef long long ll;
ll dp1[N][], dp2[N][];
int head[N], tot;
int n, k, A, B;
struct Edge{
int u, v, next;
Edge() {}
Edge(int u, int v, int next) : u(u), v(v), next(next) {}
}e[N];
void init() {
memset(head, -, sizeof head);
memset(dp1, , sizeof dp1);
memset(dp2, , sizeof dp2);
tot = ;
}
void addegde(int u, int v) {
e[tot] = Edge(u, v, head[u]);
head[u] = tot++;
}
void dfs(int u)
{
dp1[u][] = ;
for(int i = head[u]; ~i; i = e[i].next) {
int v = e[i].v;
dfs(v);
for(int j = ; j <= k; ++j) dp1[u][j] += dp1[v][j - ];
}
}
void dfs2(int u)
{
for(int i = head[u]; ~i; i = e[i].next) {
int v = e[i].v;
dp2[v][] = ; dp2[v][] = ;
for(int j = ; j <= k; ++j)
dp2[v][j] = dp1[u][j - ] - dp1[v][j - ] + dp2[u][j - ];
dfs2(v);
}
}
ll solve()
{
dfs();
for(int i = ; i <= n; ++i)
for(int j = ; j <= k; ++j)
dp1[i][j] += dp1[i][j - ];
dfs2();
ll ans = ;
for(int i = ; i <= n; ++i) ans ^= (dp1[i][k] + dp2[i][k]);
return ans;
}
int main()
{
int _; scanf("%d", &_);
while(_ --)
{
scanf("%d%d%d%d", &n, &k, &A, &B);
init();
for(int i = ; i <= n; ++i) {
int f = (int)((1ll * A * i + B) % (i - ) + );
addegde(f, i);
}
printf("%lld\n", solve());
}
return ;
}

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. CSS颜色代码 颜色值 颜色名字大全(转载)

    CSS颜色代码 颜色值 颜色名字大全 转载处http://flyjj.com/css-colour-code.html 颜色值 CSS 颜色使用组合了红绿蓝颜色值 (RGB) 的十六进制 (hex) ...

  2. poj 3735 Training little cats 矩阵快速幂+稀疏矩阵乘法优化

    题目链接 题意:有n个猫,开始的时候每个猫都没有坚果,进行k次操作,g x表示给第x个猫一个坚果,e x表示第x个猫吃掉所有坚果,s x y表示第x个猫和第y个猫交换所有坚果,将k次操作重复进行m轮, ...

  3. python2.7之MySQLdb模块 for linux安装

    1.下载:MySQL-pythonhttp://sourceforge.net/projects/mysql-python/files/mysql-python-test/1.2.3b1/MySQL- ...

  4. IOS - SDWebImage 非ARC 问题

    非arc项目中使用SDWebImage类库 1.添加类库引用    (1)ImageIO.framework    (2)MapKit.framework 2.在targets->build P ...

  5. lazyload

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  6. Oracle数据库对象题库

    一.    填空题 在用 create 语句创建基本表时,最初只是一个空的框架,用户可以使用insert命令把数据插入表中. 在基本表不需要时,可以使用 drop table 语句撤消.在一个基本表撤 ...

  7. October 3rd 2016 Week 41st Monday

    Better to light one candle than to curse the darkness. 与其诅咒黑暗,不如燃起蜡烛. Sitting in the darkness and wa ...

  8. ppt动画制作bullets

    动画->效果选项->作为一个对象 这样之后,字总是在一段时间后就自己出来,而不是我们点一下再出来,解决方法是对同一段字重复设置,后面那个会默认是点一下,出一张,在把之前的动画删除即可.

  9. android setCompoundDrawables 不显示问题

    在 vh.tvAddr.setCompoundDrawables(getResources().getDrawable(R.drawable.ic_real_state_loc), null, nul ...

  10. [转]嵌入式SQC文件编译

      Src Url:http://blog.csdn.net/cws1214/article/details/12996351   A.预编译部分  1.预编译DB2篇 1.1 什么是DB2预编译 在 ...