Bichrome Tree
Bichrome Tree
时间限制: 1 Sec 内存限制: 128 MB
题目描述
To each vertex in the tree, Snuke will allocate a color, either black or white, and a non-negative integer weight.
Snuke has a favorite integer sequence, X1,X2,…,XN, so he wants to allocate colors and weights so that the following condition is satisfied for all v.
The total weight of the vertices with the same color as v among the vertices contained in the subtree whose root is v, is Xv.
Here, the subtree whose root is v is the tree consisting of Vertex v and all of its descendants.
Determine whether it is possible to allocate colors and weights in this way.
Constraints
1≤N≤1 000
1≤Pi≤i−1
0≤Xi≤5 000
输入
N
P2 P3 … PN
X1 X2 … XN
输出
样例输入
3
1 1
4 3 2
样例输出
POSSIBLE
提示
For example, the following allocation satisfies the condition:
Set the color of Vertex 1 to white and its weight to 2.
Set the color of Vertex 2 to black and its weight to 3.
Set the color of Vertex 3 to white and its weight to 2.
There are also other possible allocations.
来源/分类
题意:给你一颗树,树上的节点可以染成黑白两色之一,要求以某点为根的子树中,颜色和根节点相同的节点的权值和为x。
先放下这两个颜色,只考虑相对情况。显然对于某一颗子树而言,它的其中一个颜色的和是定值,就是x,而另一个颜色的值可以变动。
显然贪心的使另一个颜色的值最小,这样才可能孩子节点某一颜色权值总和不超过父节点的权值。
但是当孩子节点某一颜色权值总和不超过父节点的权值时,我们又要尽可能的选择孩子节点中权值大的,这样才能保证父节点的另一个颜色和尽可能的小。
于是考虑分组背包。
#include<iostream>
#include<cstdio>
#include<vector>
#define N 1006
using namespace std;
vector<int>child[N];
int w[N]={},b[N]={};
int n,x[N]; int f(int now)
{
int len=child[now].size();
int dp[]={};
long long sum=; if(len==)
{
w[now]=x[now];
b[now]=;
return ;
} for(int i=;i<len;i++)
{
if(f(child[now][i])==-)return -;
sum+=w[child[now][i]]+b[child[now][i]];
} long long now_sum=;
for(int i=;i<len;i++)now_sum+=min(w[child[now][i]],b[child[now][i]]);
if(now_sum>x[now])return -; for(int i=;i<=x[now];i++)dp[i]=i; for(int i=;i<len;i++)
{
for(int j=x[now];j>;j--)
{
if(j>=w[child[now][i]])
{
if(dp[j-w[child[now][i]]]<=dp[j])
{
dp[j]=dp[j-w[child[now][i]]];
}
} if(j>=b[child[now][i]])
{
if(dp[j-b[child[now][i]]]<=dp[j])
{
dp[j]=dp[j-b[child[now][i]]];
}
}
}
} w[now]=x[now];
b[now]=sum-(x[now]-dp[x[now]]);
return ;
} int main()
{
int p[N];
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&p[i]);
child[p[i]].push_back(i);
}
for(int i=;i<=n;i++)scanf("%d",&x[i]); if(f()!=-)printf("POSSIBLE");
else
printf("IMPOSSIBLE"); return ;
}
Bichrome Tree的更多相关文章
- ARC083E. Bichrome Tree
A viable configuration of the given tree can be divided into two trees, each consists of vertices of ...
- 【ARC083E】Bichrome Tree
Description 给一棵\(n\)个节点的树,和一个长度同样为\(n\)的非负整数序列\(x_i\). 请尝试对每个节点染黑或白两种颜色,并确定一个非负整数权值. 问是否存在一种方案 ...
- 【ARC083E】Bichrome Tree 树形dp
Description 有一颗N个节点的树,其中1号节点是整棵树的根节点,而对于第ii个点(2≤i≤N)(2≤i≤N),其父节点为PiPi 对于这棵树上每一个节点Snuke将会钦定一种颜色(黑或白), ...
- 【BZOJ】ARC083 E - Bichrome Tree
[算法]树型DP [题意]给定含n个点的树的形态,和n个数字Xv,要求给每个点赋予黑色或白色和权值,满足对于每个点v,子树v中和v同色的点的权值和等于Xv.n<=10^5 [题解]首先每个点的权 ...
- AtCoder Regular Contest 083 E - Bichrome Tree
题目传送门:https://arc083.contest.atcoder.jp/tasks/arc083_c 题目大意: 给定一棵树,你可以给这些点任意黑白染色,并且赋上权值,现给定一个序列\(X_i ...
- [AtCoder Regular Contest 083] Bichrome Tree
树形DP. 每个点有两个属性:黑色点的权值和,白色点权值和,一个知道另一个也一定知道. 因为只要子树的和它相等的点得权值和不超过x[u],u点的权值总能将其补齐. 设计状态f[u]表示以u为根的子树, ...
- 【AtCoder】ARC083
C - Sugar Water 计算一下可以达到水是多少,可以到达的糖是多少 枚举水,然后加最多能加的糖,是\(min(F - i *100,E * 100)\),计算密度,和前一个比较就行 #inc ...
- AtCoder Regular Contest 083
C - Sugar Water Time limit : 3sec / Memory limit : 256MB Score : 300 points Problem Statement Snuke ...
- AtCoder Regular Contest 093 E: Bichrome Spanning Tree(生成树)
Bichrome Spanning Tree 题意: 给出一个n个点,m条边的无向连通图,现在要给每条边染色,可以染成黑色或者白色. 现在要求在染色完毕后,找出一个至少包含一条黑边和一条白边的最小生成 ...
随机推荐
- 前端知识点总结——HTML
HTML:HTML4.01 指的就是网页技术HTML5:HTML4.01的升级版本 1.web的基础知识 web与Internet1.Internet:全球性的计算机互联网络,因特网,互联网,交互网2 ...
- 获取CPU相关信息
实现效果: 知识运用: WMI管理类中的ManagementObjectCollection类 ManagementObjectSearcher类的Get方法 和ManagementObje ...
- CPP-基础:wchar_t
目 录 1简介 2例如 3将char转换成wchar_t 1.简介 wchar_t是C/C++的字符数据类型,是一种扩展的字符存储方式,wchar_t类型主要用在国际化程序的实现中,但它不等同于uni ...
- Springboot 命令注入属性[--]&[-D]
场景 在用Jenkins,做自动化部署时,遇到一些命令问题. 需要通过命令的形式,注入些业务值. -D 系统属性注入 Java,启动jar 命令: java [ options ] -jar file ...
- kubernetes添加不了google apt-key
转自icepoint的博客 key来源 我的百度云盘 密码:v3wo 下载kube_apt_key.gpg到本地,上传到服务器后执行下面的命令 apt-get update && ap ...
- Noip2011提高组 聪明的质监员
题目传送门 讲真,既然质监员这么聪明,为什么要让我们帮他设计程序? 所以还是叫ZZ的质检员吧 其实,我最想说的,不是这个题,而是这个\(\Sigma\)(一见 \(\Sigma\) 就懵逼系列) 这个 ...
- 如何让升级时AppleHDA不再折腾
---前提--- 1. 你得用 Clover 引导 (......) 2. 开启 kernelcache (开了也能 inject kext,还能patch kext,速度又快,为啥不开) 3. 你的 ...
- 前端vue 里的tab切换 减少dom操作
<div class="vuedemo"> <div class="all"> <div class="tabone&q ...
- sessionStorage 的数据会在同一网站的多个标签页之间共享吗?这取决于标签页如何打开
一直以来,我所以为的 sessionStorage 的生命周期是这样的:在 sessionStorage 中存储的数据会在当前浏览器的同一网站的多个标签页中共享,并在此网站的最后一个标签页被关闭后清除 ...
- 830. Positions of Large Groups@python
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...