题意:
给出一棵树,每个节点都被标记了黑或白色,要求把这棵树的其中k条变切换,划分成k+1棵子树,每颗子树必须有1个黑色节点,求有多少种划分方法。
题解:
树形dp
dp[x][0]表示是以x为根的树形成一块不含黑色点的方案数
dp[x][1]表示是以x为根的树形成一块含一个黑色点方案数
//зїеп: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的更多相关文章

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

    B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

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

  3. Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】

    题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...

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

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

  6. Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)

    题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...

  7. Educational Codeforces Round 58 (Rated for Div. 2) D 树形dp + 数学

    https://codeforces.com/contest/1101/problem/D 题意 一颗n个点的树,找出一条gcd>1的最长链,输出长度 题解 容易想到从自底向长转移 因为只需要g ...

  8. CodeCraft-19 and Codeforces Round #537 (Div. 2) E 虚树 + 树形dp(新坑)

    https://codeforces.com/contest/1111/problem/E 题意 一颗有n个点的树,有q个询问,每次从树挑出k个点,问将这k个点分成m组,需要保证在同一组中不存在一个点 ...

  9. Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)

    https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...

随机推荐

  1. Vimer的福音 新时代的Vim C++自动补全插件 clang_complete

    使用vim的各位肯定尝试过各种各样的自动补全插件,比如说大名鼎鼎的 OmniCppComplete .这一类的插件都是对 Ctags 生成的符号表进行字符串匹配来获得可能的补全项.他们在编写 C 代码 ...

  2. sql批量获取wordpress所有留言者的邮件地址

    如果你的wordpress博客有很多读者互动的话,他们的留言都会留下具体的联系邮箱,我们如何批量导出这些联系信息呢?可以试试下面的sql语句 SELECT DISTINCT comment_autho ...

  3. Word Amalgamation(枚举 + 排序)

    Word Amalgamation Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 373  Solved: 247 Description In mil ...

  4. cocos2dx的内存管理机制

    首先我们必须说一下c++中变量的内存空间的分配问题,我们在c++中写一个类,可以在栈上分配内存空间也可以使用new在堆上分配内存空间,如果类对象是在栈上分配的内存空间,这个内存空间的管理就不是我们的事 ...

  5. 淘宝(阿里百川)手机客户端开发日记第七篇 Service,Handler和Thread

    现在我们已经已经知道android有Service,Handler和Thread这些内容了,但是我想应该还有很多人对此并不是很清楚他们之间的区别! (1)Service 是运行在后端的程序,不与UI直 ...

  6. HDU 1062 Text Reverse(水题,字符串处理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062 解题报告:注意一行的末尾可能是空格,还有记得getchar()吃回车符. #include< ...

  7. 在服务器上远程链接另一台服务器的数据库的方法how to connet the database from the other host

    iwangzheng.com 16:57 [root@a02.cmsapi]$ mysql -u<username> -p<password> -h10.103.xx.xx W ...

  8. [ruby on rails] 跟我学之(8)修改数据

    修改views 修改index视图(app/views/posts/index.html.erb),添加编辑链接,如下: <h1>Our blogs</h1> <% @p ...

  9. lz4,pigz,gzip 3者比较

    一.压缩(1.1)使用gzip进行打包:# time tar -zcf tar1.tar binlog*real 0m48.497suser 0m38.371ssys 0m2.571s (1.2)使用 ...

  10. 一个很不错的适合PHPER们书单,推荐给大家【转】

    来我博客的访客们中,有一些是PHP的初学者,是不是很迷茫PHP应该怎么学?应该买什么样的书?到处问人,到处求助?这下好了. 正好看到黑夜路人在博客上推荐了一个书单,看上去都非常不错,很多我也没有读过, ...