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]\) ...
随机推荐
- php-fpm.conf两个至关重要的参数
这里规定了PHP-CGI的连接.发送和读取的时间,300秒足够用了,因此我的服务器很少出现504 Gateway Time-out这个错误.最关键的是php-fpm.conf的设置,这个会直接导致50 ...
- Socket网络编程(2)--服务端实现
中秋了,首先祝大家中秋快乐,闲着无事在家整一个socket的聊天程序,有点仿QQ界面,就是瞎折腾,不知道最后是不是能将所有功能实现. 如果你对socket不了解,请看这篇文章:http://www.c ...
- The Flash
flash.now[:error] = "" render :new flash[:error] = "" redirect videos_path http: ...
- linux zookeeper 原理详解
http://cailin.iteye.com/blog/2014486 直接打开此链接即可 -------------------------------------------------- ...
- Python Django 的 django templatedoesnotexist
django 1.8版本的解决方案 在 setting.py 这个文件里 TEMPLATES = [ ...... #原来的 #'DIRS': [ ], // 这个 列表里添加 template路 ...
- MyBatis调用存储过程
MySQL存储过程 DROP PROCEDURE IF EXISTS transferMoney; -- 实现转账功能的存储过程 CREATE PROCEDURE transferMoney ( IN ...
- jlink安装
https://www.segger.com/jlink-software.html?step=1&file=JLink_510p
- 利用cocostudio库函数 实现左右滑动的背包栏UI (cocos2d-x 2.2.0)
.h #ifndef __COMMON_COMPONENTS__ #define __COMMON_COMPONENTS__ #include "cocos2d.h" #inclu ...
- 【转】MySQL Temporary Table相关问题的探究
本文转载自:http://itindex.net/detail/10901-mysql-temporary-table 问题的引入 让我们先来观察几条非常简单的MySQL语句: mysql> c ...
- mysql in 查询优化
2014年11月29日21:01:01 场景:有的时候查询数据库的select in 语句中会有非常多不连续的数值,会很影响查询效率 方法:将select in 查询转换成多个select betwe ...