题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5593

题意:

http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=654&pid=1004

题解:

先自底向上跑一遍,求出以u为根的子树中与u距离小于等于k的节点数(不同的距离要分开存,否则无法递推上去,dp[u][i]存距离为i的节点数)

求好之后,再dfs一遍,这次换做自顶向下,目的是求u的兄弟(和兄弟的后代)以及u的祖先(和祖先的后代,不包括以u为根的子树)中与u距离小于等于k的节点数(也是分开存)

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn=5e5+;
typedef long long LL; int dp[maxn][];
int fa[maxn];
int N,K,A,B; struct Edge{
int v,ne;
Edge(int v,int ne):v(v),ne(ne){}
Edge(){}
}egs[maxn*]; int head[maxn],tot; void addEdge(int u,int v){
egs[tot]=Edge(v,head[u]);
head[u]=tot++;
}
//从下往上跑一遍
void dfs1(int u){
int p=head[u];
while(p!=-){
Edge& e=egs[p];
dfs1(e.v);
for(int i=;i<=K;i++){
//转移方程1:
dp[u][i]+=dp[e.v][i-];
}
p=e.ne;
}
dp[u][]=;
}
//从上往下跑一遍
void dfs2(int u){
if(fa[u]){
for(int i=K;i>=;i--){
//转移方程2:
dp[u][i]+=dp[fa[u]][i-]-dp[u][i-];//dp[fa[u]][i-1]有包括u本身,所以要扣掉u的那部分对fa[u]的贡献
}
dp[u][]++;
}
int p=head[u];
while(p!=-){
Edge& e=egs[p];
dfs2(e.v);
p=e.ne;
}
} void init(){
fa[]=;
memset(dp,,sizeof(dp));
memset(head,-,sizeof(head));
tot=;
} int main(){
int tc;
scanf("%d",&tc);
while(tc--){
scanf("%d%d%d%d",&N,&K,&A,&B);
init();
for(int i=;i<=N;i++){
int x=((LL)A*i+B)%(i-)+;
addEdge(x,i);
fa[i]=x;
}
dfs1();
dfs2();
LL ans=;
for(int i=;i<=N;i++){
int cnt=;
for(int j=;j<=K;j++) cnt+=dp[i][j];
ans^=cnt;
}
printf("%lld\n",ans);
}
return ;
}

HDU 5593 ZYB's Tree 树形dp的更多相关文章

  1. Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp

    Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

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

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

  3. hdu5593/ZYB's Tree 树形dp

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

  4. hdu 4514 并查集+树形dp

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

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

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

  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 4003 Find Metal Mineral 树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003 Humans have discovered a kind of new metal miner ...

  8. hdu6035 Colorful Tree 树形dp 给定一棵树,每个节点有一个颜色值。定义每条路径的值为经过的节点的不同颜色数。求所有路径的值和。

    /** 题目:hdu6035 Colorful Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:给定一棵树,每个节点有一个颜色值.定 ...

  9. hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)

    题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: ...

随机推荐

  1. WCF学习笔记(一)

    WCF是什么? 官方解释: Windows Communication Foundation (WCF) 是用于构建面向服务的应用程序的框架.借助 WCF,可以将数据作为异步消息从一个服务终结点发送至 ...

  2. CentOS6.X安装vsftpd服务

    #-----------------CentOS6.X安装VSFTPD服务 #! /bin/sh #1.关闭selinux setenforce 0 sed -i 's/enforcing/disab ...

  3. iOS中UIKit——UIButton设置边框

    UIButton *testButton = [UIButton buttonWithType:UIButtonTypeSystem]; [testButton setFrame:CGRectMake ...

  4. [leetcode]_Path Sum I && II

    都是考查DFS.经典回溯算法,问题在于我对该类型的代码不熟悉,目前以参考别人的代码,然后加上自己的实现为主,通过类似的题目加强理解. 一.给定一棵二叉树,判断是否存在从root到leaf的路径和等于给 ...

  5. 利用脚本修改SQL SERVER排序规则

    利用脚本修改SQL SERVER排序规则 编写人:CC阿爸 2014-3-1 l  今年的一项重要工作是对公司所用系统进行繁简的转换,程序转成简体基本很容易解决,但数据库转换成简体,就没那么容易了.经 ...

  6. luigi学习1

    一.luigi介绍 luigi是基于python语言的,可帮助建立复杂流式批处理任务管理系统.这些批处理作业典型的有hadoop job,数据库数据的导入与导出,或者是机器学习算法等等. luigi的 ...

  7. source insight用于C语言编程的工具脚本

    简单来说,source insight提供的功能功能还不够傻瓜,用起来还不够方便,所以写了此脚本,提高开发效率. 部分source insight提供的功能也包含了进来,主要是因为我不喜欢使用太多的快 ...

  8. xml结构

    一.XmlHelper using System; using System.Collections.Generic; using System.Linq; using System.Web; usi ...

  9. Learning Scrapy笔记(零) - 前言

    我已经使用了scrapy有半年之多,但是却一直都感觉没有入门,网上关于scrapy的文章简直少得可怜,而官网上的文档(http://doc.scrapy.org/en/1.0/index.html)对 ...

  10. 在EF的code frist下写稳健的权限管理系统:数据库模型(二)

    先从数据库开始,因为是用EF的code frist,所以所有的设计都在解决项目中进行. 先是数据模型开始 我已经建立了四个模型,user,role,action,actiongroup user里面有 ...