CodeForces 461B Appleman and T
题目链接:http://codeforces.com/contest/461/problem/B
题目大意:
给定一课树,树上的节点有黑的也有白的,有这样一种分割树的方案,分割后每个子图只含有一个黑色节点,问这样的分割方案一共有多少种?
分析:
先定义3个函数(为了之后说起来方便):
设B(x) = 在以顶点x为根的子树中,所有满足每个子图只含有1个黑色顶点的分割方案种数 。
设C(x) = 在以顶点x为根的子树中,所有满足使x所在子图只含有1个黑色顶点,其余子图只含有1个黑色顶点的分割方案种数 。
代码如下:
#include <bits/stdc++.h>
using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define pii pair<int,int>
#define piii pair<pair<int,int>,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
const int inf = 1e9 + ;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ; struct Node{
vector< int > next;
}; int n, ans; /*
设A(x) = 在以顶点x为根的子树中,所有满足使x所在子图不含黑色顶点,其余子图只含有1个黑色顶点的分割方案种数
设B(x) = 在以顶点x为根的子树中,所有满足每个子图只含有1个黑色顶点的分割方案种数
设C(x) = 在以顶点x为根的子树中,所有满足使x所在子图只含有1个黑色顶点,其余子图只含有1个黑色顶点的分割方案种数
则有:
dp[v][0] = A(v) + B(v)
dp[v][1] = B(v)
C(v) = B(v)
The answer is dp[0][1].
*/
LL dp[maxN][];
Node nodes[maxN];
int color[maxN]; void dfs(int x) {
// 默认顶点x为白色
dp[x][] = ;
dp[x][] = ; foreach(i, nodes[x].next) {
dfs(*i);
// dp[*i][0] 包含 A(*i) 和 C(*i),对于A(*i),令其与C(x)相连
// 而对于C(*i),令其与C(x)分断
dp[x][] = (dp[x][] * dp[*i][]) % mod;
// dp[x][0] 包含 A(x),令其与C(*i)相连
dp[x][] = (dp[x][] + dp[x][] * dp[*i][]) % mod;
// 此时dp[x][0] = A(x)
// dp[x][0] * dp[*i][0] = A(x)*A(*i) + A(x)*C(*i)
// 依次是连接新子树的A(*i)部分和断掉新子树的C(*i)部分,两这都可以保证dp[x][0]更新后还是A(x)
dp[x][] = (dp[x][] * dp[*i][]) % mod;
} if(color[x] == ) dp[x][] = dp[x][]; // 当根节点为黑色时,A(x)实际上是C(x),而真正的A(x) = 0
else dp[x][] = (dp[x][] + dp[x][]) % mod; // 此时 dp[x][0] = A(x) + C(x)
} int main(){
while(cin >> n) {
rep(i, n) nodes[i].next.clear();
For(i, , n-) {
int t;
cin >> t;
// 根据题目要求,并不需要加反向边
nodes[t].next.push_back(i);
}
rep(i, n) cin >> color[i]; dfs(); cout << dp[][] <<endl;
}
return ;
}
CodeForces 461B Appleman and T的更多相关文章
- Codeforces 461B Appleman and Tree(木dp)
题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...
- Codeforces 461B. Appleman and Tree[树形DP 方案数]
B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 461B Appleman and Tree
http://codeforces.com/problemset/problem/461/B 思路:dp,dp[i][0]代表这个联通块没有黑点的方案数,dp[i][1]代表有一个黑点的方案数 转移: ...
- Codeforces 461B Appleman and Tree:Tree dp
题目链接:http://codeforces.com/problemset/problem/461/B 题意: 给你一棵树(编号从0到n-1,0为根节点),每个节点有黑白两种颜色,其中黑色节点有k+1 ...
- Codeforces 461B - Appleman and Tree 树状DP
一棵树上有K个黑色节点,剩余节点都为白色,将其划分成K个子树,使得每棵树上都仅仅有1个黑色节点,共同拥有多少种划分方案. 个人感觉这题比較难. 如果dp(i,0..1)代表的是以i为根节点的子树种有0 ...
- CodeForces 462B Appleman and Card Game(贪心)
题目链接:http://codeforces.com/problemset/problem/462/B Appleman has n cards. Each card has an uppercase ...
- codeforces 462C Appleman and Toastman 解题报告
题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得 ...
- 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 263B. Appleman and Card Game
B. Appleman and Card Game time limit per test 1 second memory limit per test 256 megabytes input ...
随机推荐
- Windows系统 应用或游戏 打开出现0xc000007b错误 解决方法
1.使用directX修复工具(推荐) 标准版 增强版 标准版备用地址 增强版备用地址 2. 重新安装DirectX 9.0 安装包(安装包体积大) 微软官方离线安装包 摘录CSDN博客 运行游戏时出 ...
- 讲讲跳跃表(Skip Lists)
跳跃表(Skip Lists)是一种有序的数据结构,它通过在每个节点中维持多个指向其他节点的指针,从而达到快速访问节点的目的.在大部分情况下,跳跃表的效率可以和平衡树相媲美,并且在实现上比平衡树要更为 ...
- elementui el-upload 在v-for里使用时 如何获取index
<div v-for = 'item in list'> <div @click="getImageTypeIndex(index)"> <el-up ...
- cmd实现批量文件的base64加密并双击加密文件后正常运行
之前,一个朋友让我用cmd做个简单的文档加密(base64),对于不太懂电脑的人来说看不懂就行.但是当那个人点击加密后的文件可以正常运行,问咋写? 其实,像这种要求不高的加密来说,随便下载个加密软件就 ...
- Linux简单配置SendMail发送邮件
本文简单整理了一下如何在Linux服务器上安装.配置SendMail发送邮件的步骤,此文不是配置邮件服务器,具体测试环境为CentOS Linux release 7.2.1511 (Core) ,如 ...
- 【Spring】application.xml文件配置
什么是Spring? Spring是分层的javaEE full-stack(一站式)轻量级开源框架. ---注解配置--针对SSM <?xml version="1.0" ...
- C 语言 IO 缓存 相关
必要了解函数的功能和使用场景: fflush, setbuf, setvbuf 了解的操作: setbuf(stdout,NULL); // 关闭输出缓冲区: libc 和 linux 内核IO缓存模 ...
- Cs231n课堂内容记录-Lecture 7 神经网络训练2
Lecture 7 Training Neural Networks 2 课堂笔记参见:https://zhuanlan.zhihu.com/p/21560667?refer=intelligent ...
- WPF中在MVVM模式下,后台绑定ListCollectionView事件触发问题
问题:WPF中MVVM模式下 ListView绑定ListCollectionView时,CurrentChanged无法触发 解决方案: 初期方案:利用ListView的SelectionChang ...
- Linux Collection:系统信息和配置
PAS Linux系统配置相关文件 /etc 目录中是系统和各类软件的配置文件 /var/log 系统日志 /proc 系统运行信息 PAS /boot空间不足 查看内核 dpkg --get-sel ...