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 ...
随机推荐
- C/C++招聘的一些感受和经验【转】
找工作本人认为最重要的就是前期准备了. 首先.简历一定要写的切合主题.招聘单位要的是你的技能,这个只要大概能符合就可以,关键他们需要的是你的开发经验,一定要在简历中完美的体现出你之前所参与的项 ...
- 3D模型选中显示样式改变
osg::ref_ptr<osg::Material> material = new osg::Material(); //模型材质信息 material->setTranspare ...
- IE WebDriver 因保护模式无法启动的解决 (转载)
现在Win7 已经应用很多了,即使是最原始的Win7 也是IE8,最新的patch后,都升到了IE11 Win7下预装高版本IE的情况下,启动IE WebDriver可能会出现: org.openqa ...
- 製程能力介紹(SPC introduction) ─ 製程能力的三種表示法
製程能力的三種表示法 Ck: 準度指標 (accuracy) Ck=(M-X)/(T/2) Cp: 精度指標 (precision) Cp=T/(6σp) 規格為單邊時:Cp=(Tu-X)/3 ...
- Xcode 4.1~4.6 + iOS 5、iOS 6免证书(iDP)开发+真机调试+生成IPA全攻略
原创文章,欢迎分享:未经许可,不得转载:版权所有,侵权必究 开发环境使用的是目前为止最新的稳定版软件:Mac OS X Lion 10.7 + Xcode 4.1 目前Xcode 4.2 Previe ...
- RedHat Enterprise Linux 6.3 安装Oracle Database 11g
按照以下文章正确将oracle安装在linux上 http://yiyiboy2010.iteye.com/blog/1670795 http://mirrors.163.com/centos/6.5 ...
- Emotional Mastery——英语学习小技巧之一
How can we control or manage our emotion ,so that we feel better and feel stronger when we're learni ...
- 调用父类Controller错误
在写一个控制器的时候,要特别注意本类继承的父类.不要继承错了.如图: ,这样就会一直是显示父类的控制器,而不是显示本类的控制器视图. 应该改为: 这些都是平时遇到的一些小问题,留着提醒自己.
- PLSQLDeveloper过期要注册表
打开运行输入 regedit 打表注册表 删除 HKEY_CURRENT_USER\Software\Allround Automations HKEY_CURRENT_USER\Software\M ...
- MySQL学习笔记(2)
打开数据库 USE db_name; SELECT DATABASE();查看当前所选中的数据库 创建数据表 CREATA TABLE [IF NOT EXISTS] table_name ( col ...