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

本文介绍了一种利用树形DP算法解决特定类型距离求和问题的方法。具体而言,给定一棵拥有N个节点的树,对于每个节点i,求解树中离其距离不超过K的节点数量,并将所有节点的答案进行异或操作得到最终结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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=(Ai+B)%(i1)+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

1T5,1N500000,1K10,1A,B1000000
 

 

Output
For  T lines,each line print the ans.

Please open the stack by yourself.

N100000 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 = 500005;
typedef long long ll;
ll dp1[N][12], dp2[N][12];
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, -1, sizeof head);
    memset(dp1, 0, sizeof dp1);
    memset(dp2, 0, sizeof dp2);
    tot = 0;
}
void addegde(int u, int v) {
    e[tot] = Edge(u, v, head[u]);
    head[u] = tot++;
}
void dfs(int u)
{
    dp1[u][0] = 1;
    for(int i = head[u]; ~i; i = e[i].next) {
        int v = e[i].v;
        dfs(v);
        for(int j = 1; j <= k; ++j) dp1[u][j] += dp1[v][j - 1];
    }
}
void dfs2(int u)
{
    for(int i = head[u]; ~i; i = e[i].next) {
        int v = e[i].v;
        dp2[v][0] = 0; dp2[v][1] = 1;
        for(int j = 2; j <= k; ++j)
        dp2[v][j] = dp1[u][j - 1] - dp1[v][j - 2] + dp2[u][j - 1];
        dfs2(v);
    }
}
ll solve()
{
    dfs(1);
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= k; ++j)
        dp1[i][j] += dp1[i][j - 1];
    dfs2(1);
    ll ans = 0;
    for(int i = 1; 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 = 2; i <= n; ++i) {
            int f = (int)((1ll * A * i + B) % (i - 1) + 1);
            addegde(f, i);
        }
        printf("%lld\n", solve());
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/orchidzjl/p/5026818.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值