Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 354 Accepted Submission(s): 100
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.
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
Please open the stack by yourself.
N≥100000 are only for two tests finally.
#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的更多相关文章
- HDU 5593 ZYB's Tree 树形dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5593 题意: http://bestcoder.hdu.edu.cn/contests/contes ...
- Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- HDU5593 ZYB's Tree 树形DP +分治
感觉其实就是树分治,一次BC的题,感觉这次题目质量比较高,仅代表蒟蒻的看法 一次DFS获取每个点到子树的距离不大于K的点的个数, 然后一遍BFS获取从每个点父亲不大于K的的个数,层层扩展,还是想说 其 ...
- hdu5593/ZYB's Tree 树形dp
ZYB's Tree Memory Limit: 131072/131072 K (Java/Others) 问题描述 ZYBZYB有一颗NN个节点的树,现在他希望你对于每一个点,求出离每个点距 ...
- codeforces Round #263(div2) D. Appleman and Tree 树形dp
题意: 给出一棵树,每个节点都被标记了黑或白色,要求把这棵树的其中k条变切换,划分成k+1棵子树,每颗子树必须有1个黑色节点,求有多少种划分方法. 题解: 树形dp dp[x][0]表示是以x为根的树 ...
- HUD 5593——ZYB's Tree——————【树形dp】
ZYB's Tree Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tota ...
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- 熟练剖分(tree) 树形DP
熟练剖分(tree) 树形DP 题目描述 题目传送门 分析 我们设\(f[i][j]\)为以\(i\)为根节点的子树中最坏时间复杂度小于等于\(j\)的概率 设\(g[i][j]\)为当前扫到的以\( ...
- BestCoder Round #65
博弈 1002 ZYB's Game 题意:中文 分析:假定两个人是绝顶聪明的,一定会采取最优的策略.所以如果选择X的左边的一个点,那么后手应该选择X的右边对称的点,如果没有则必输,否则必胜,然后再分 ...
随机推荐
- 【leetcode】Remove Duplicates from Sorted Array I & II(middle)
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 【leetcode】Number of Islands(middle)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- UVA 10815 Andy's First Dictionary ---set
题目链接 题意:输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出.单词不区分大小写. 刘汝佳算法竞赛入门经典(第二版)P112 #include <iostream> ...
- 51nod 1605 棋盘问题 (博弈)
题目:传送门. 题意:中文题.T组数据,每组给定一个n*m的棋盘,棋盘中的1代表黑色,0代表白色,每次可以将1或者非2质数的全黑色方形区域变为白色,不能操作者输,问谁能赢. 题解:每次可以将1或者非2 ...
- dropdownlist 动态添加
this.DropDownList1.Items.Insert(0,new ListItem("","")); this.Drop ...
- myaql常用函数
一.数学函数 abs(x) 返回x的绝对值 bin(x) 返回x的二进制(oct返回八进制,hex返回十六进制) ceiling(x) 返回大于x的最小整数值 exp(x) 返回值e( ...
- mysql可以用这种方式<<! 输入内容 ! 做成脚本
以这种文件式做交接NB!!!!! [root@NB test]# mysql -uroot -p$passwd <<! > use mysql > select user,ho ...
- Delphi管理多线程之线程局部存储:threadvar
尽管多线程能够解决许多问题,但是同时它又给我们带来了很多的问题.其中主要的问题就是:对全局变量或句柄这样的全局资源如何访问?另外,当必须确保一个线程中的某些事件要在另一个线程中的其他时间之前(或之后) ...
- C# 一些常用的技巧代码
1.字符串风格成字符数组: 比如将字符串:23$123$45$转换成int[]这样的数组,你该怎么转换?其实你不用写那么的for循环,只需要一句话: int [] Relst =Array.Conve ...
- 算法系列:geometry
1.基本几何变换及变换矩阵 基本几何变换都是相对于坐标原点和坐标轴进行的几何变换,有平移.比例.旋转.反射和错切等. 1.1 平移变换 是指将p点沿直线路径从一个坐标位置移到另一个坐标位置的重定位过程 ...