Codeforces Round #263
http://codeforces.com/contest/461
A.水题
B.太挫了,竟然被hack了一发。。。。
C.贪心。。竟然没看出来时哈夫曼编码问题
D.题目大意:给一棵树,每一个点为白色或黑色,切断一些边,使得每一个连通块有且仅有一个黑点,问划分方案数。
树形DP,
设状态:
dp[v][0]表示以v为根的子树中没有黑点,dp[v][1]有一个黑点。
状态方程:
dp[v][1] = dp[v][1]*dp[son][0] + dp[v][0]*dp[son][1] + dp[v][1]*dp[son][1]
dp[v][0] = dp[v][0]*dp[son][0] + dp[v][0]*dp[son][1]
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod=1000000007;
const int maxn = 100000 + 100;
vector<int> g[maxn];
int clr[maxn];
LL dp[maxn][2];
int n; void dfs(int u, int p)
{
dp[u][clr[u]] = 1;
for(int i=0; i<g[u].size(); ++i)
{
int &v = g[u][i];
if(v==p) continue;
dfs(v, u);
dp[u][1] = (dp[u][1]*dp[v][0]%mod + dp[u][0]*dp[v][1]%mod + dp[u][1]*dp[v][1]%mod) % mod;
dp[u][0] = (dp[u][0]*dp[v][1]%mod + dp[u][0]*dp[v][0]%mod) % mod;
}
}
int main()
{
scanf("%d", &n);
int x;
for(int i=1; i<n; ++i)
{
scanf("%d",&x);
g[i].push_back(x);
g[x].push_back(i);
}
for(int i=0; i<n; ++i)
{
scanf("%d", &clr[i]);
} dfs(0, -1);
printf("%I64d\n", dp[0][1]);
return 0;
}
E.
Div1 D Appleman and Complicated Task
Div1 E Appleman and a Game
Codeforces Round #263的更多相关文章
- 贪心 Codeforces Round #263 (Div. 2) C. Appleman and Toastman
题目传送门 /* 贪心:每次把一个丢掉,选择最小的.累加求和,重复n-1次 */ /************************************************ Author :R ...
- Codeforces Round #263 (Div. 1)
B 树形dp 组合的思想. Z队长的思路. dp[i][1]表示以i为跟结点的子树向上贡献1个的方案,dp[i][0]表示以i为跟结点的子树向上贡献0个的方案. 如果当前为叶子节点,dp[i][0] ...
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...
- codeforces Round #263(div2) D. Appleman and Tree 树形dp
题意: 给出一棵树,每个节点都被标记了黑或白色,要求把这棵树的其中k条变切换,划分成k+1棵子树,每颗子树必须有1个黑色节点,求有多少种划分方法. 题解: 树形dp dp[x][0]表示是以x为根的树 ...
- Codeforces Round #263 (Div. 2)
吐槽:一辈子要在DIV 2混了. A,B,C都是简单题,看AC人数就知道了. A:如果我们定义数组为N*N的话就不用考虑边界了 #include<iostream> #include &l ...
- Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)
题目链接 D. Appleman and Tree time limit per test :2 seconds memory limit per test: 256 megabytes input ...
- Codeforces Round #263 (Div. 2) A B C
题目链接 A. Appleman and Easy Task time limit per test:2 secondsmemory limit per test:256 megabytesinput ...
- Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新
C. Appleman and a Sheet of Paper Appleman has a very big sheet of paper. This sheet has a form of ...
- Codeforces Round #263 (Div. 2) proC
题目: C. Appleman and Toastman time limit per test 2 seconds memory limit per test 256 megabytes input ...
随机推荐
- js-计算器
<div class="main"><h1>HTML5-计算器</h1> <input id="num1& ...
- hdu 5730 Shell Necklace fft+cdq分治
题目链接 dp[n] = sigma(a[i]*dp[n-i]), 给出a1.....an, 求dp[n]. n为1e5. 这个式子的形式显然是一个卷积, 所以可以用fft来优化一下, 但是这样也是会 ...
- IP地址、子网掩码和地址分类
http://blog.csdn.net/bluishglc/article/details/47909593?utm_source=tuicool&utm_medium=referral 实 ...
- 用OxyPlot在WPF中演示正演磁异常的变化规律
为了在展示实验成果时动态演示理论球体磁异常随其埋深.磁化倾角的变化规律,我用WPF写了一个小程序来作演示. MatLab计算磁异常数据 首先是计算理论球体磁异常数据,在Matlab中可以很方便地计算. ...
- Xcode Coule not launch "aaa" press launch failed:timed out waiting for app launch
遇见这个问题 可能是 由于 runapp 的时候设置里面 设置为release了. 解决办法是:见图 build configuration 设置成 debug 状态就OK了. 要是上面的不行就试一下 ...
- canvas1
canvas学习(一) Canvas 学习之路 (一) canvas 是H5 里面神一样的东西,使得只是通过html和js就能做出非常棒的游戏和画面. 因为对前端无限的爱好,更加对canvas充满好奇 ...
- SQL Server 全文索引
create table Document(ID int not null,Name nvarchar(255) not null,Body nvarchar(max) not null);go cr ...
- 奇怪的haproxy 跳转
<pre name="code" class="html">奇怪的Haproxy 跳转: acl admin_req path_beg -i /ad ...
- Linux-storage-stack-diagram
just a diagram 一目了然. 对于isci 只是用过LIO和STGT 两种后端. 这里有各种后端的比较. http://scst.sourceforge.net/comparison.ht ...
- Data Visualization 课程 笔记1
对数据可视化比较有兴趣,因此最近在看coursera上伊利诺伊大学香槟分校的数据可视化课程,做了一些笔记. 1. 定义 Data visualization is a high bandwidth c ...