codeforces Round #263(div2) D. Appleman and Tree 树形dp

//зїеп:1085422276
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
#include <stack>
typedef long long ll;
#define inf 100000000
#define mod 1000000007
using namespace std; inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//*************************************** struct ss
{ int to,next;
}e[*];
ll dp[][];
int head[],t,vis[],cl[];
void init(){
memset(head,,sizeof(head));
t=;
}
void add(int u,int v)
{
e[t].next=head[u];
e[t].to=v;
head[u]=t++;
}
void dfs(int x,int pre)
{
//vis[x]=1;
dp[x][cl[x]]=;
for(int i=head[x];i;i=e[i].next)
{
if(e[i].to==pre)continue;
dfs(e[i].to,x);
if(cl[x]==){
dp[x][]=(dp[x][]*dp[e[i].to][]%mod+dp[x][]*dp[e[i].to][])%mod;
}
else { dp[x][]=(dp[x][]*dp[e[i].to][]%mod+dp[x][]*dp[e[i].to][]%mod+dp[x][]*dp[e[i].to][])%mod;
dp[x][]=(dp[x][]*dp[e[i].to][]%mod+dp[x][]*dp[e[i].to][])%mod;
} }
} int main()
{
init();
int n;
scanf("%d",&n);
int x;
for(int i=;i<n-;i++){
scanf("%d",&x);
add(i+,x);
add(x,i+);
}
for(int i=;i<n;i++)
scanf("%d",&cl[i]);
dfs(,-);
cout<<dp[][]<<endl;
return ;
}
代码
codeforces Round #263(div2) D. Appleman and Tree 树形dp的更多相关文章
- Codeforces 461B. Appleman and Tree[树形DP 方案数]
B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 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 ...
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...
- 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 ...
- Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)
题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...
- Educational Codeforces Round 58 (Rated for Div. 2) D 树形dp + 数学
https://codeforces.com/contest/1101/problem/D 题意 一颗n个点的树,找出一条gcd>1的最长链,输出长度 题解 容易想到从自底向长转移 因为只需要g ...
- CodeCraft-19 and Codeforces Round #537 (Div. 2) E 虚树 + 树形dp(新坑)
https://codeforces.com/contest/1111/problem/E 题意 一颗有n个点的树,有q个询问,每次从树挑出k个点,问将这k个点分成m组,需要保证在同一组中不存在一个点 ...
- Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)
https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...
随机推荐
- B0BO TFS 安装指南(转载)
TFS2008安装过几次,每次都遇到点麻烦,结合网上的一些经验总结一下: Windows SharePoint Services 安装 Windows SharePoint Services你有两个选 ...
- 重温设计模式(三)——职责链模式(chain of responsibility)
一. 写在前面的 这么多的设计模式,我觉得职责链是我第一次看上去最简单,可是回想起来却又最复杂的一个模式. 因此,这个文章我酝酿了很久,一直也没有胆量发出来,例子也是改了又改,可是仍然觉得不够合理.所 ...
- 普通用户如何临时获取root权限
转自:http://634871.blog.51cto.com/624871/1325907 在实际工作中,公司不会将root用户直接给员工使用,而是通过员工自己的账号临时获得系统的root权限. 1 ...
- CentOS6.5安装iftop
iftop这个小工具是Linux和unix下的top命令升级版,功能相对较强,界面易懂.今天安装了CentOS6.5的最新版,装个小工具检查下系统运行性能. 官网:http://www.ex-parr ...
- CF440C
C. One-Based Arithmetic time limit per test 0.5 seconds memory limit per test 256 megabytes input st ...
- Wormholes(Bellman-ford)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 33008 Accepted: 12011 Descr ...
- cocos基础教程(2)Window环境下搭建
第一步:开始安装VS2012 第二步:下载Cocos2d-x 3.4源码 配置环境变量 COCOS_CONTROL = E:\cocos2d-x-3.4\tools\cocos2d-console ...
- Mysql limit offset
Mysql limit offset 假设数据库表student存在13条数据. 语句1:select * from student limit 9,4 语句2:slect * from studen ...
- 一些Linux的路径
系统引导时启动 /etc/rc.d/rc.local
- Ubuntu14.04server开放rootssh登录权限
刚安装了Ubuntu 14.04 server的虚拟机,普通帐号可以远程登录,但是root不行,输入密码后一直报错: permission denied 最后发现ssh的配置(/etc/ssh/ssh ...