Codeforces Round #263 (Div.1) B. Appleman and Tree
来源:互联网 发布:淘宝家具安装加盟平台 编辑:程序博客网 时间:2024/05/08 12:23
题目地址:http://codeforces.com/contest/461/problem/B
题目大意:给一棵树,每个点为白色或黑色,切断一些边,使得每个连通块有且仅有一个黑点,问划分方案数。
算法讨论:TreeDP。f[x][0..1]表示x所在连通块有0/1个黑点。设y为x的儿子,则DP方程为f[x][1]=f[x][1]*f[y][0]+f[x][1]*f[y][1]+f[x][0]*f[y][1],f[x][0]=f[x][0]*f[y][0]+f[x][0]*f[y][1]。
Code:
#include <cstdio>#define N 100000#define mod 1000000007using namespace std;long long f[N+10][2];int n,x,mm,next[N+10],son[N+10],ed[N+10],c[N+10];inline void add(int x,int y){next[++mm]=son[x],son[x]=mm,ed[mm]=y;}void dfs(int x){f[x][c[x]]=1;for (int i=son[x];i;i=next[i]){int y=ed[i];dfs(y);f[x][1]=(f[x][1]*f[y][0]%mod+f[x][1]*f[y][1]%mod+f[x][0]*f[y][1]%mod)%mod;f[x][0]=(f[x][0]*f[y][0]%mod+f[x][0]*f[y][1]%mod)%mod;}}int main(){scanf("%d",&n);for (int i=2;i<=n;++i) scanf("%d",&x),add(++x,i);for (int i=1;i<=n;++i) scanf("%d",&c[i]);dfs(1);printf("%I64d\n",f[1][1]);return 0;}
By Charlie Pan
Aug 27,2014
0 0
- Codeforces Round #263 (Div.1) B. Appleman and Tree
- Codeforces Round #263 (Div. 1) B. Appleman and Tree
- Codeforces Round #263 (Div. 1 - B) Appleman and Tree
- Codeforces Round #263 (Div. 1) B. Appleman and Tree
- Codeforces Round #263 (Div. 1) B. Appleman and Tree( 树形DP )
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
- Codeforces Round #263 (Div. 2)B. Appleman and Card Game
- Codeforces Round #263 (Div. 2)B. Appleman and Card Game
- Codeforces Round #263 (Div. 2)-B. Appleman and Card Game
- Codeforces Round #263 (Div. 1) A. Appleman and Toastman
- Codeforces Round #263 (Div. 2)A. Appleman and Easy Task
- Codeforces Round #263 (Div. 2)C. Appleman and Toastman
- Codeforces Round #263 (Div. 2)A. Appleman and Easy Task
- Codeforces Round #263 (Div. 2) C. Appleman and Toastman
- Codeforces Round #263 (Div. 2)-C. Appleman and Toastman
- Codeforces Round #263 (Div. 2)-C. Appleman and Toastman
- Codeforces Round #263 Appleman and Tree(树形DP)
- Codeforces 461 B. Appleman and Tree
- Android文件Apk下载变ZIP压缩包解决方案
- android自定义View
- restful的实例论证
- Active Record
- c#基于事件模型的UDP通讯框架(适用于网络包编解码)
- Codeforces Round #263 (Div.1) B. Appleman and Tree
- sQLite问题:Error cannot start a transaction within a transaction
- Codefores Round #263 (div2) B. Appleman and Card Game
- The 4 Levels of Freedom For Software Developers
- java专题——栈stack应用
- hadoop namenode ha--手动切换
- groovy与java交互
- Android中shape的使用
- subString的用法