zoukankan      html  css  js  c++  java
  • CodeForces 416 B Appleman and Tree DP

    Appleman and Tree

    题解:

    定义dp[u][1] 为以u的子树范围内,u这个点已经和某个黑点相连的方案数。

    dp[u][0] 为在u的子树范围内, u这个点还未和某个黑点相连的方案数。

    转移方程:

    如果 u为黑点, dp[u][0] = 0, dp[u][1] = 1, 然后考虑从下面转移过来, dp[u][1] *= dp[v][0] + dp[v][1].

    也就是说, 如果 v 点为黑,则切断这个边, 如果v点为白,则不切断, 即对于v来说,每个情况,切边的情况也只有一种, 不同的v的方案数相互独立。

    如果 u为白点, dp[u][0] = 1, dp[u][1] = 1, 考虑转移 dp[u][0] *= dp[v][0] + dp[v][1], dp[u][1] += dp[v][1] * (除v以外的子树(dp[z][1] + dp[z][0])乘积 。

    对于dp[u][0]来说,和上面的道理一样。

    对于dp[u][1]来说,枚举和下面哪一个黑点连边,然后这个点对于其他的v来说就相当于一个黑点,转移的方程就是和 u 是黑点的道理一样了。

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
    #define LL long long
    #define ULL unsigned LL
    #define fi first
    #define se second
    #define pb push_back
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define lch(x) tr[x].son[0]
    #define rch(x) tr[x].son[1]
    #define max3(a,b,c) max(a,max(b,c))
    #define min3(a,b,c) min(a,min(b,c))
    typedef pair<int,int> pll;
    const int inf = 0x3f3f3f3f;
    const int _inf = 0xc0c0c0c0;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const LL _INF = 0xc0c0c0c0c0c0c0c0;
    const LL mod =  (int)1e9+7;
    const int N = 1e5 + 100;
    vector<int> vc[N];
    int dp[N][2];
    int pre[N], suf[N];
    int a[N];
    void dfs(int u){
        if(a[u] == 1) {
            dp[u][1] = 1; dp[u][0] = 0;
            for(int v : vc[u]){
                dfs(v);
                dp[u][1] = (1ll * dp[u][1] * (dp[v][1] + dp[v][0])) % mod;
            }
        }
        else{
            dp[u][1] = 0; dp[u][0] = 1;
            int t = vc[u].size();
            for(int v : vc[u]){
                dfs(v);
            }
            for(int i = 0; i < t; ++i){
                int v = vc[u][i];
                suf[i+1] = pre[i+1] = (dp[v][0]+dp[v][1]) % mod;
            }
            pre[0] = 1; suf[t+1] = 1;
            for(int i = 1; i <= t; ++i)
                pre[i] = 1ll * pre[i] * pre[i-1] % mod;
            for(int i = t; i >= 1; --i)
                suf[i] = 1ll * suf[i] * suf[i+1] % mod;
            dp[u][0] = pre[t];
            for(int i = 1; i <= t; ++i){
                int v = vc[u][i-1];
                dp[u][1] = (dp[u][1] + 1ll * dp[v][1] * (1ll * pre[i-1] * suf[i+1]%mod))% mod;
            }
        }
    }
    int main(){
        int n, o;
        scanf("%d", &n);
        for(int i = 2; i <= n; ++i){
            scanf("%d", &o);
            vc[o+1].pb(i);
        }
        for(int i = 1; i <= n; ++i)
            scanf("%d", &a[i]);
        dfs(1);
        printf("%d
    ", dp[1][1]);
        return 0;
    }
    View Code
  • 相关阅读:
    [转载] kill命令
    [转载] Linux的Top命令解析
    json互相转换
    C# 13位时间戳(unix时间戳)
    第八篇:cx_Oracle出现的问题
    linux:eth网卡对应的物理网口判断
    ls显示前几行或后几行数据
    第七篇:suds.TypeNotFound: Type not found: '(string, http://schemas.xmlsoap.org/soap/encoding/, )'
    第六篇:python中numpy.zeros(np.zeros)的使用方法
    第五篇:selenium调用IE问题(Protected Mode settings are not the same for all zones)
  • 原文地址:https://www.cnblogs.com/MingSD/p/10832290.html
Copyright © 2011-2022 走看看