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\n", dp[1][1]);
    return 0;
}

原文地址:https://www.cnblogs.com/MingSD/p/10832290.html

时间: 2024-11-18 17:19:23

CodeForces 416 B Appleman and Tree DP的相关文章

codeforces 514E Darth Vader and Tree (dp+快速幂)

codeforces 514E Darth Vader and Tree (dp+快速幂) 题意: 有一棵树,每个节点有n个儿子,给出父亲到每个儿子的距离di,问离祖先距离不超过x的子孙有多少个(子孙包括祖先)对1e9+7取模. 限制: 1 <= n <= 1e5; 0 <= x <= 1e9; 1 <= di <= 100 思路: 因为di <= 100,所以可以用快速幂来处理这道题, 大概过程为:先用dp算出前100的答案,剩下的用快速幂来处理.

Codeforces 461 B. Appleman and Tree

树形DP... B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices a

Codeforces 486D Valid Sets:Tree dp【n遍O(n)的dp】

题目链接:http://codeforces.com/problemset/problem/486/D 题意: 给你一棵树,n个节点,每个节点的点权为a[i]. 问你有多少个连通子图,使得子图中的max(a[i]) - min(a[i]) <= d. ps.连通子图的定义: 如果一个点集V为一个连通子图,则对于任意两点a,b∈V,有a到b路径上的所有点u∈V. 题解: 因为要保证max(a[i]) - min(a[i]) <= d,所以可以人为地选出一个点rt作为点权最大的点. 这样在求以rt

Codeforces 461B Appleman and Tree(木dp)

题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k条边,使得树变成k+1个联通分量.保证每一个联通分量有且仅有1个黑色节点.问有多少种切割方法. 解题思路:树形dp,dp[i][0]和dp[i][1]分别表示子树一下的切割方法中,i节点所在联通块不存在黑节点和已经存在一个黑节点的方案数. #include <cstdio> #include &l

Codeforces 461B Appleman and Tree(树形dp)

题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每个节点的父亲节点,以及每个点的颜色(0表示白色,1表示黑色),切断这棵树的k条边,使得树变成k+1个联通分量,保证每个联通分量有且仅有1个黑色节点.问有多少种分割方法. 解题思路:树形dp,dp[i][0]和dp[i][1]分别表示子树一下的分割方法中,i节点所在联通块不存在黑节点和已经存在一个黑节点的方案数. #include <cstdio> #include <c

Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)

题目链接 D. Appleman and Tree time limit per test :2 seconds memory limit per test: 256 megabytes input :standard input output:standard output Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices a

Codeforces 461B. Appleman and Tree[树形DP 方案数]

B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are color

CF461B Appleman and Tree (树DP)

CF462D Codeforces Round #263 (Div. 2) D Codeforces Round #263 (Div. 1) B B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Appleman has a tree with n vertices. Some of t

CF 461B Appleman and Tree 树形DP

Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white. Consider a set consisting of k (0 ≤ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then