ZYB's Tree

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

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
 
 
 

题目大意:给你一棵无根树,定义了结点距离为任意结点对(i,j)在最短路径上的边的条数。让你求出每个结点的结点距离
小于等于K的结点个数,然后求异或值。
解题思路:我们的总体思路是,先任意定一个根,形成一棵有根树,对于任意的结点,我们可以从距离该结点为K的儿孙结点
和祖先结点两个方面去统计结点个数。我们定义dp[u][j]表示以某个结点u为根的子树中,距离小于等于j的结点个数。现在我们
已经解决了距离该结点为K的儿孙方面的个数,还有祖先方面的需要统计,那么我们可以从结点u向他的祖先结点走,会经过u的父亲
祖父,曾祖父...依次向上。向上走i层,该祖先结点表示为ff,第i-1 层的祖先结点表示为pff。

那么会增加dp[ff][K-i] - dp[pff][K-i-1]个结点。
另外:题中给的1结点的父亲结点0是不用参与运算的。当到达0结点时,可以跳出循环。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
const int maxn = 550000;
typedef long long LL;
int N,K,A,B;
int f[maxn], dp[maxn][20];
vector<int>G[maxn];
void dfs(int u,int fa){
for(int i = 0; i <= K; i++){ //初始化以u为子树的根,距离u为i时的结点个数为1
dp[u][i] = 1;
}
for(int i = 0; i < G[u].size(); i++){
int v = G[u][i];
if(v == fa) continue;
dfs(v,u);
for(int j = 0; j <= K; j++){ //统计u的所有子节点
dp[u][j+1] += dp[v][j];
}
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d%d",&N,&K,&A,&B);
for(int i = 0; i <= N+20; i++){
G[i].clear();
}
f[1] = 0;
for(int i = 2; i <= N; i++){
int ff = (int)(((LL)A*i+B)%((LL)i-1) + 1); //计算时会超int
f[i] = ff;
G[ff].push_back(i);
}
memset(dp,0,sizeof(dp));
dfs(1,0);
int ans = 0, tmp;
for(int u = 1; u <= N; u++){
tmp = dp[u][K]; //子孙方面个数
int ff = u; //初始化祖先结点
for(int i = 1; i <= K; i++){
if(i == K){
tmp += dp[f[ff]][K-i]; continue;
}
if(f[ff] == 0){ //已经到了0结点
break;
}
tmp += dp[f[ff]][K-i] - dp[ff][K-i-1]; //统计向上经过的满足条件的祖先会贡献多少个结点
ff = f[ff]; //向祖先走
}
ans ^= tmp; //求每个结点的异或和
}
printf("%d\n",ans);
}
return 0;
}
/* 55
15 3 3 8 */

  

 

HUD 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. HDU 5593 ZYB's Tree 树形dp

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

  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. 熟练剖分(tree) 树形DP

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

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

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

  8. codeforces 161D Distance in Tree 树形dp

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

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

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

随机推荐

  1. js 值传递,引用传递

    参考:http://www.cnblogs.com/lcngu/p/5876273.html JS的基本类型,是按值传递的. 对象类型按共享传递的(call by sharing,也叫按对象传递.按对 ...

  2. findControl 可以获取前台页面的控件

    findControl 可以获取前台页面的控件

  3. P4011 孤岛营救问题

    \(\color{#0066ff}{题目描述}\) 1944 年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛,营救被敌军俘虏的大兵瑞恩.瑞恩被关押在一个迷宫里,迷宫地形复杂,但幸好麦克 ...

  4. 状压DP 【洛谷P3694】 邦邦的大合唱站队

    [洛谷P3694] 邦邦的大合唱站队 题目背景 BanG Dream!里的所有偶像乐队要一起大合唱,不过在排队上出了一些问题. 题目描述 N个偶像排成一列,他们来自M个不同的乐队.每个团队至少有一个偶 ...

  5. java Pattern和Matcher完全解析

    基本使用: 本文不讲解正则表达式,需要请看API Scanner中的使用正则表达式 //Scanner 支持的分组 Scanner cin=new Scanner("red a bbc&qu ...

  6. POJ 1000 A+B

    #include <stdio.h> int main() { int a,b; scanf("%d %d",&a, &b); printf(" ...

  7. C/C++中qsort()以及sort()的用法

    最近学弟们问快速排序的比较多,今天自己就做一下总结,快速排序在库函数里面有现成的,不用自己实现,调用一下就可以达到自己想要的结果,掌握以后就可以完全摒弃冒泡和选择了,并且时间复杂度也从O(n*n)提升 ...

  8. P3796 【模板】AC自动机

    传送门 AC自动机的模板 简单的理解就是字典树上的KMP 注意数组不要开太大 不然每次memset耗时太多 有一个小优化 每次走 fail 边找匹配时只有一些会更新答案 那么就可以把没用的fail边压 ...

  9. redis 学习字符类型 hash

    > HSET userinfo1 username "king" (integer) > HSET userinfo1 passowor " (integer ...

  10. ZPL通用打印类

    using System;using System.Collections.Generic;using System.IO;using System.Runtime.InteropServices;u ...