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

BestCoder Round #65

题意:给出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;
}

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

时间: 2024-12-26 06:50:43

Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp的相关文章

Bestcoder round #65 &amp;&amp; hdu 5592 ZYB&#39;s Premutation 线段树

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 74 Problem Description ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutat

hdu5593/ZYB&#39;s Tree 树形dp

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

BestCoder Round #65 hdu5592 5593 5594

hdu5592 - ZYB's Premutation http://acm.hdu.edu.cn/showproblem.php?pid=5592 对于一个X=a[i]-a[i-1],它可以表示为原排列第i个位置的数,在它前面有X个比它大的数,也就是说在1到i的区间内第i个数为该区间内第X+1大的数 那么可以考虑倒着求出原排列:对于最后一个数,令k=a[n]-a[n-1],最后一个数就是k+1:而对于倒数第二个数,令k'=a[n-1]-a[n-2],则该位置的答案为从1到n中除去k,第k'+1

Codeforces Round #168 (Div. 1) B. Zero Tree 树形dp

题目链接: http://codeforces.com/problemset/problem/274/B 题意: 给出一棵树,每个点有权值,每次操作可以对一个联通子集中的点全部加1,或者全部减1,且每次操作必须包含点1,问最少通过多少次操作可以让整棵树每个点的权值变为0. 思路: http://blog.csdn.net/qq_24451605/article/details/48622953 定义状态up[u],down[u]代表点u被加操作的次数和点u被减操作的次数 因为必须包含点1,所以我

BestCoder Round #65 (ZYB&#39;s Game)

ZYB's Game Accepts: 672 Submissions: 1207 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description ZYBZYBZYB played a game named NumberBombNumber BombNumberBomb with his classmates in hiking:a host keeps a

[BestCoder Round #3] hdu 4909 String (状压,计数)

String Problem Description You hava a non-empty string which consists of lowercase English letters and may contain at most one '?'. Let's choose non-empty substring G from S (it can be G = S). A substring of a string is a continuous subsequence of th

[BestCoder Round #4] hdu 4932 Miaomiao&#39;s Geometry (贪心)

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 363    Accepted Submission(s): 92 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by

[BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)

Task schedule Problem Description 有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务. 有m个询问,每个询问有一个数字q,表示如果在q时间有一个工作表之外的任务请求,请计算何时这个任务才能被执行. 机器总是按照工作表执行,当机器空闲时立即执行工作表之外的任务请求. Input 输入的第一行包含一个整数T, 表示一共有T组测试数据. 对于每组测试数据: 第一行是两个数字n, m,表示工作表里面有n个任务,

[BestCoder Round #4] hdu 4931 Happy Three Friends

Happy Three Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 70    Accepted Submission(s): 62 Problem Description Dong-hao , Grandpa Shawn , Beautful-leg Mzry are good friends. One day ,