ZYB's Tree

 

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

两个点(x,y)(x,y)在树上的距离定义为两个点树上最短路径经过的边数,

为了节约读入和输出的时间,我们采用如下方式进行读入输出:

读入:读入两个数A,BA,B,令fa_ifa​i​​为节点ii的父亲,fa_1=0fa​1​​=0;fa_i=(A*i+B)\%(i-1)+1fa​i​​=(A∗i+B)%(i−1)+1 i \in [2,N]i∈[2,N] .

输出:输出时只需输出NN个点的答案的xorxor和即可。
输入描述
第一行一个整数TT表示数据组数。

接下来每组数据:

 一行四个正整数N,K,A,BN,K,A,B.

 最终数据中只有两组N \geq 100000N≥100000。

1 \leq T \leq 51≤T≤5,1 \leq N \leq 5000001≤N≤500000,1 \leq K \leq 101≤K≤10,1 \leq A,B \leq 10000001≤A,B≤1000000
输出描述
TT行每行一个整数表示答案.
输入样例
1
3 1 1 1
输出样例
3

 题解:定义dp[i][j]为以i为根距离为j的点的个数

定义dp2[i][j] 在除去i的子树的点中,与点i距离为j的点的个数

在遍历图求出dp[][]后

对于fa,son

我们求dp2的转移方程就是

dp2[son][h]=dp[fa][h-1]-dp[son][h-2]+dp[fa][h-1];

//meek
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <sstream>
#include <vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//**************************************** const int N=+;
const ll inf = 1ll<<;
const int mod= ; int a,b,K,n;
int vis[N];
vector<int >G[N];
int nex;
int dp[N][],dp2[N][];
void dfs(int x) {
dp[x][]=;
for(int i=;i<G[x].size();i++) {
dfs(G[x][i]);
for(int j=;j<=K;j++) {
dp[x][j]+=dp[G[x][i]][j-];
}
} }
int main() {
int T;
scanf("%d",&T);
while(T--) {
mem(dp),mem(dp2);
scanf("%d%d%d%d",&n,&K,&a,&b);
for(int i=;i<=N;i++) G[i].clear();
for(int i=;i<=n;i++) {
ll fa=(a+b)%(i-)+;
G[fa].pb(i);
}int A=,ans;
dfs();
for(int i=;i<=n;i++) {
for(int j=;j<G[i].size();j++) {
dp2[G[i][j]][]=dp[i][];
for(int h=;h<=K;h++)
dp2[G[i][j]][h]=dp[i][h-]-dp[G[i][j]][h-]+dp2[i][h-];
}
ans=;
for(int j=;j<=K;j++) {
ans+=dp[i][j]+dp2[i][j];
}
A^=ans;
}
printf("%d\n",A);
}
return ;
}

代码

hdu5593/ZYB's Tree 树形dp的更多相关文章

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

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

  2. 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 ...

  3. HDU 5593 ZYB's Tree 树形dp

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

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

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

  5. 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: ...

  6. CF 461B Appleman and Tree 树形DP

    Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...

  7. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

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

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

  9. 5.10 省选模拟赛 tree 树形dp 逆元

    LINK:tree 整场比赛看起来最不可做 确是最简单的题目. 感觉很难写 不过单独考虑某个点 容易想到树形dp的状态. 设f[x]表示以x为根的子树内有黑边的方案数. 白边方案只有一种所以不用记录. ...

随机推荐

  1. Eclipse常用快捷键使用

    Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键.     1. [ALT+/]     此快捷键为用户编辑的好帮手 ...

  2. golang的nil

    golang中什么样的类型可以赋值nil? 类型文档中有注定"uninitialized value is nil"的类型都可以赋值nil. golang的基本类型不能赋值nil: ...

  3. [转]linux时间同步

    转自:http://www.jotop.com/2012/vpsinfo_0525/439.html 美国的vps大多都是国外的时间,让我们的程序总是不适应.那么如何调整linux的时间为北京时间?修 ...

  4. Swift 中的利刃,函数和闭包

    input[type="date"].form-control,.input-group-sm>input[type="date"].input-grou ...

  5. angularjs resources

    http://tutorials.jenkov.com/angularjs/watch-digest-apply.html http://angular-tips.com/blog/2013/08/w ...

  6. 四则运算三+psp0级表格

    一.题目 在四则运算二的基础上,选择一个方向进行拓展,我选择的是增加了答题模块 二.设计思路 1.在上次的基础上,增加了答题模块,每出现一道四则运算题目,便提醒输入结果,如果结果错误,就会提示错误 2 ...

  7. 软件工程随堂小作业——随机四则运算Ⅱ(C++)

    一.设计思路 设计思路已给出,此处不再赘述. 二.源代码 (1)四则运算2.cpp(源文件) // 四则运算2.cpp : Defines the entry point for the consol ...

  8. kibana 修改Ico图标

    修改此路径下的E:\happy\kinbana\kibana-4.2.2-windows\kibana-4.2.2-windows\optimize\bundles的commons.bundle.js ...

  9. 【Recover Binary Search Tree】cpp

    题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...

  10. linux 安装memcached

    1.下载文件wget http://www.monkey.org/~provos/libevent-1.4.12-stable.tar.gzwget http://www.memcached.org/ ...