HDU 5593 (树DP)

ZYB's Tree

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


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
 
题意是给出一棵树,树的边权都是1,求出每个点,和它的距离小于k的点的数量,最后求所有的这些结果的异或和.
刚开始写的时候以为是树分治,写了一半卡住了,一看50W的数据量nlgnlgn的复杂度简直自杀,然后发现k只有10=.=
那就很简单了一遍dfs,dp[i][j]表示i为根的子树中距离它为j的有多少个节点,然后往上的距离为j的也很简单,假设i的父亲是u,dp[u][j]-dp[i][j-1]就是不在i为根的子树上的点满足的个数,然后u不断的用u的父亲更新,向上k次或者到根.
复杂度O(n*k).
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
#define maxn 511111
#define maxm 1111111

long long n, a, b;
int k;
long long dp[maxn][15];
struct node {
    int u, v, next;
}edge[maxm];
int cnt, head[maxn], fa[maxn];
long long ans[maxn];

void add_edge (int u, int v) {
    edge[cnt].u = u, edge[cnt].v = v, edge[cnt].next = head[u], head[u] = cnt++;
    return ;
}

int d[maxn];
void dfs (int u, int deep) {
    d[u] = deep;
    dp[u][0] = 1;
    for (int i = head[u]; i != -1; i = edge[i].next) {
        int v = edge[i].v;
        dfs (v, deep+1);
        for (int i = 1; i <= k; i++) {
            dp[u][i] += dp[v][i-1];
        }
    }
}

long long cal (int u) {
    int pre = u;
    long long ans = 0;
    for (int i = 0; i <= k; i++) {
        ans += dp[u][i];
    }
    int tot = 1;
    while (u != 0 && tot <= k) {
        int father = fa[u];
        if (father == 0)
            break;
        for (int i = 1; i <= k-tot; i++) {
            ans += dp[father][i] - dp[u][i-1];
        }
        u = father; tot++;
    }
    ans += d[pre]-d[u];
    return ans;
}

void solve () {
    memset (d, 0, sizeof d);
    memset (dp, 0, sizeof dp);
    dfs (1, 1);
    memset (ans, 0, sizeof ans);
    for (int i = 1; i <= n; i++) {
        ans[i] = cal (i);
    }
    long long num = 0;
    for (int i = 1; i <= n; i++)
        num^= ans[i];
    printf ("%lld\n", num);
}

int main () {
    //freopen ("in", "r", stdin);
    int t;
    scanf ("%d", &t);
    while (t--) {
        scanf ("%lld%lld%lld%lld", &n, &k, &a, &b);
        memset (head, -1, sizeof head);
        cnt = 0;
        fa[1] = 0;
        for (int i = 2; i <= n; i++) {
            fa[i] = (a*i+b)%(i-1)+1;
            add_edge (fa[i], i);
        }
        solve ();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值