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

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 ;
}
上一篇:信息摘要算法之四:SHA512算法分析与实现


下一篇:json转换为键值对辅助类