Codeforces 462D Appleman and Tree 树形dp

题目链接:点击打开链接

题意:

给定n个点的树,

0为根,下面n-1行表示每个点的父节点

最后一行n个数 表示每个点的颜色,0为白色,1为黑色。

把树分成若干个联通块使得每个联通块有且仅有一个黑点,问有多少种分法(结果mod1e9+7)

思路:

树形dp,每个点有2个状态,已经归属于某个黑点和未归属于某个黑点。

#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
#define N 300100
#define mod 1000000007
typedef long long ll;
ll dp[N][2];
//dp[i][1]表示i点已经归属于某个黑点的方法数
//dp[i][0]表示i点未曾归属某个黑点的方法数
int n, col[N];
vector<int>G[N];
void dfs(int u){
	dp[u][1] = col[u];
	dp[u][0] = col[u]^1;
	for(int i = 0; i < G[u].size(); i++){
		int v = G[u][i];
		dfs(v);
		ll old[2] = {dp[u][0], dp[u][1]};
		dp[u][0] = (old[0]*dp[v][1] +old[0]*dp[v][0])%mod;
		dp[u][1] = (old[1]*dp[v][1] +old[1]*dp[v][0] +old[0]*dp[v][1])%mod;
	}
}
int main() {
    int i, j;
    while(~ scanf("%d",&n)){
		for(i = 0; i < n; i++)G[i].clear();
		for(i = 1; i < n; i++){
			scanf("%d",&j);
			G[j].push_back(i);
		}
		for(i = 0; i < n; i++)scanf("%d",&col[i]);
		dfs(0);
		cout<<dp[0][1]<<endl;
    }
    return 0;
}
/*
5
0 1 1 2
1 0 0 1 1

*/
时间: 2024-10-10 14:09:57

Codeforces 462D Appleman and Tree 树形dp的相关文章

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 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

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

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 Round #263 (Div. 2) D. Appleman and Tree 树形dp

链接: http://codeforces.com/contest/462/problem/D 题意: 给定n个点的树, 0为根,下面n-1行表示每个点的父节点 最后一行n个数 表示每个点的颜色,0为白色,1为黑色. 把树分成若干个联通块使得每个联通块有且仅有一个黑点,问有多少种分法(结果mod1e9+7) 题解: 树形dp,每个点有2个状态,已经归属于某个黑点和未归属于某个黑点. 代码: 31 int n; 32 int x[MAXN]; 33 VI G[MAXN]; 34 ll dp[MAX

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

Bestcoder round #65 &amp;&amp; hdu 5593 ZYB&#39;s Tree 树形dp

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 354    Accepted Submission(s): 100 Problem Description ZYB has a tree with N nodes,now he wants you to solve the numbers of nodes distanced no m

codeforces161D - Distance in Tree 树形dp

题意:给你一棵树,问你树中距离为k的有多少种情况. 解题思路:树形dp  维护每个节点(1-K)深度的情况, 解题代码: 1 // File Name: 161d.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月03日 星期日 19时20分10秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #incl

hdu5593/ZYB&#39;s Tree 树形dp

ZYB's Tree Memory Limit: 131072/131072 K (Java/Others) 问题描述 ZYBZYB有一颗NN个节点的树,现在他希望你对于每一个点,求出离每个点距离不超过KK的点的个数. 两个点(x,y)(x,y)在树上的距离定义为两个点树上最短路径经过的边数, 为了节约读入和输出的时间,我们采用如下方式进行读入输出: 读入:读入两个数A,BA,B,令fa_ifa?i??为节点ii的父亲,fa_1=0fa?1??=0;fa_i=(A*i+B)\%(i-1)+1fa