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. java调用cmd命令删除文件夹及其所有内容

    /** * *删除D盘下面test目录,感觉以前用io流遍历删除好慢! * **/ public static void main(String[] args) { Runtime run = Run ...

  2. IOS- 堆和栈 详解

    Objective-C的对象在内存中是以堆的方式分配空间的,并且堆内存是由你释放的,即release 栈由编译器管理自动释放的,在方法中(函数体)定义的变量通常是在栈内,因此如果你的变量要跨函数的话就 ...

  3. 执行HQL语句出现Remember that ordinal parameters are 1-based

    今天使用hibernate出个奇怪的错误,第一次碰到 Remember that ordinal parameters are 1-based! 原因是 问题发生的原因是:hql语句里不需要参数,却添 ...

  4. JDBC题库

    一.    填空题 JDBC    ,是一种用于执行SQL语句的Java API,为多种关系数据库提供统一访问.它由一组用Java语言编写的类和接口组成. JDBC API:供程序员调用的接口与类,集 ...

  5. 使用cocoapods导入第三方类库后 头文件没有代码提示?

    选择Target -> Build Settings 菜单,找到\”User Header Search Paths\”设置项 新增一个值"${SRCROOT}",并且选择\ ...

  6. October 11th 2016 Week 42nd Tuesday

    A friend is one who knows you and loves you just the same. 朋友就是懂你并爱你的人. Leave nothing for tomorrow w ...

  7. SQL Server 数据类型

    数据类型的选择帮助优化查询,比如针对int类型列和针对文本类型列可能会生成完全不同的查询计划 三种数据类型: 系统数据类型 别名数据类型:用户可以为系统数据类型提供一个别名,并且可以对数据类型做进一步 ...

  8. openURL的使用方法:

    openURL的使用方法: view plaincopy toclipboardprint?        [[UIApplication sharedApplication] openURL:[NS ...

  9. NYOJ题目10505C?5S?

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAscAAAJ/CAIAAAAbDelhAAAgAElEQVR4nO3dPXLbOhfG8XcT7r0Q11

  10. 三、jQuery--jQuery基础--jQuery基础课程--第4章 jQuery表单选择器

    1.:input表单选择器 如何获取表单全部元素?:input表单选择器可以实现,它的功能是返回全部的表单元素,不仅包括所有<input>标记的表单元素,而且还包括<textarea ...