zoukankan      html  css  js  c++  java
  • codeforces 416B. Appleman and Tree 树形dp

    题目链接

    Fill a DP table such as the following bottom-up:

    • DP[v][0] = the number of ways that the subtree rooted at vertex v has no black vertex.
    • DP[v][1] = the number of ways that the subtree rooted at vertex v has one black vertex.

    The recursion pseudo code is folloing:

    DFS(v):
     DP[v][0] = 1
     DP[v][1] = 0
     foreach u : the children of vertex v
      DFS(u)
      DP[v][1] *= DP[u][0]
      DP[v][1] += DP[v][0]*DP[u][1]
      DP[v][0] *= DP[u][0]
     if x[v] == 1:
      DP[v][1] = DP[v][0]
     else:
      DP[v][0] += DP[v][1]

    The answer is DP[root][1]

    #include <iostream>
    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #include <set>
    #include <string>
    #include <queue>
    #include <stack>
    #include <bitset>
    using namespace std;
    #define pb(x) push_back(x)
    #define ll long long
    #define mk(x, y) make_pair(x, y)
    #define lson l, m, rt<<1
    #define mem(a) memset(a, 0, sizeof(a))
    #define rson m+1, r, rt<<1|1
    #define mem1(a) memset(a, -1, sizeof(a))
    #define mem2(a) memset(a, 0x3f, sizeof(a))
    #define rep(i, n, a) for(int i = a; i<n; i++)
    #define fi first
    #define se second
    typedef pair<int, int> pll;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int mod = 1e9+7;
    const int inf = 1061109567;
    const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
    const int maxn = 1e5+2;
    ll dp[maxn][2];
    int head[maxn], num, k, a[maxn];
    struct node
    {
        int to, nextt;
    }e[maxn*2];
    void add(int u, int v) {
        e[num].to = v, e[num].nextt = head[u], head[u] = num++;
    }
    void init() {
        num = 0;
        mem1(head);
    }
    void dfs(int u, int fa) {
        dp[u][1] = 0;
        dp[u][0] = 1;
        for(int i = head[u]; ~i; i = e[i].nextt) {
            int v = e[i].to;
            if(v == fa)
                continue;
            dfs(v, u);
            dp[u][1] = dp[u][1]*dp[v][0]%mod;
            dp[u][1] = (dp[u][1]+dp[u][0]*dp[v][1]%mod)%mod;
            dp[u][0] = dp[u][0]*dp[v][0]%mod;
        }
        if(a[u]) {
            dp[u][1] = dp[u][0];
        } else {
            dp[u][0] = (dp[u][0]+dp[u][1])%mod;
        }
    }
    int main()
    {
        int n, x, y;
        cin>>n;
        init();
        for(int i = 1; i<n; i++) {
            scanf("%d", &x);
            add(x, i);
            add(i, x);
        }
        for(int i = 0; i<n; i++)
            scanf("%d", &a[i]);
        dfs(0, -1);
        cout<<dp[0][1]<<endl;
        return 0;
    }
  • 相关阅读:
    各种网址
    (转)quick-cocos2d-x基于源码加密打包功能的更新策略(3)
    (转)quick-cocos2d-x基于源码加密打包功能的更新策略(2)
    (转)quick-cocos2d-x基于源码加密打包功能的更新策略(1)
    (转)在lua中递归删除一个文件夹
    (转)quick-cocos2d-x图片资源加密(续)
    (转)让quick-cocos2d-x支持加密的plist文件
    (转)quick-cocos2d-x图片资源加密
    (转)quick-cocos2d-x源文件编译及加密详解
    (转)quick cocos2dx学习记录
  • 原文地址:https://www.cnblogs.com/yohaha/p/5262846.html
Copyright © 2011-2022 走看看