zoukankan      html  css  js  c++  java
  • hdu 5593 ZYB's Tree 树形dp

    ZYB's Tree

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=5593

    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

    HINT

    题意

     

    题解:

    我们维护两个dp,dp1[i][j]表示以i为根节点的子树,有多少个点在距离为j的位置

    dp2[i][j]表示并不是在子树的,有多少个离他距离为j的点

    首先很容易的在树上维护处dp1[i][j]

    然后我们再扫一遍用容斥做出dp2就好了

    代码:

    #include<iostream>
    #include<stdio.h>
    #include<vector>
    #include<cstring>
    using namespace std;
    #define maxn 500050
    vector<int> E[maxn];
    int dp[maxn][12];
    int dp2[maxn][12];
    
    int n,k,A,B;
    void dfs(int x)
    {
        dp[x][0]=1;
        for(int i=0;i<E[x].size();i++)
        {
            dfs(E[x][i]);
            for(int j=0;j<k;j++)
                dp[x][j+1]+=dp[E[x][i]][j];
        }
    }
    int main()
    {
        int t;scanf("%d",&t);
        for(int cas=1;cas<=t;cas++)
        {
            memset(dp,0,sizeof(dp));
            scanf("%d%d%d%d",&n,&k,&A,&B);
            for(int i=0;i<=n;i++)
                E[i].clear();
            for(int i=2;i<=n;i++)
            {
                int x = i;
                int y = (1LL*A+B*1LL)%(i*1LL-1)+1;
                E[y].push_back(x);
            }
            dfs(1);
            int Ans = 0;
            for(int x=1;x<=n;x++)
            {
                for(int i=0;i<E[x].size();i++)
                {
                    int y = E[x][i];
                    for(int j=0;j<=k;j++)
                        dp2[y][j]=0;
                    dp2[y][1]=dp[x][0];
                    for(int j=1;j<k;j++)
                        dp2[y][j+1]=dp[x][j]-dp[y][j-1]+dp2[x][j];
                }
                int now = 0;
                for(int i=0;i<=k;i++)
                    now+=dp[x][i]+dp2[x][i];
                Ans^=now;
            }
            printf("%d
    ",Ans);
        }
    
    }
  • 相关阅读:
    EnjoyCSS – 在线的,先进的 CSS3 代码生成器
    使用 Google Analytics 跟踪 JavaScript 错误
    免费 PSD 素材:25个全新的界面设计资源
    Framework7 – 赞!功能齐全的 iOS7 App 前端框架
    CutJS – 用于 HTML5 游戏开发的 2D 渲染引擎
    【干货分享】Node.js 中文资料导航
    酷站设计:2014年3月份获奖网站作品欣赏
    Magic CSS3 – 创建各种神奇的交互动画效果
    优秀示例:一组创意的手机注册和登录界面设计
    酷站设计!15个扁平风格网站作品欣赏
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5023387.html
Copyright © 2011-2022 走看看