Bichrome Tree

时间限制: 1 Sec  内存限制: 128 MB

题目描述

We have a tree with N vertices. Vertex 1 is the root of the tree, and the parent of Vertex i (2≤i≤N) is Vertex Pi.
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

输入

Input is given from Standard Input in the following format:
N
P2 P3 … PN
X1 X2 … XN

输出

If it is possible to allocate colors and weights to the vertices so that the condition is satisfied, print POSSIBLE; otherwise, print IMPOSSIBLE.

样例输入

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.

来源/分类

ABC074&ARC083


题意:给你一颗树,树上的节点可以染成黑白两色之一,要求以某点为根的子树中,颜色和根节点相同的节点的权值和为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的更多相关文章

  1. ARC083E. Bichrome Tree

    A viable configuration of the given tree can be divided into two trees, each consists of vertices of ...

  2. 【ARC083E】Bichrome Tree

    Description ​ 给一棵\(n\)个节点的树,和一个长度同样为\(n\)的非负整数序列\(x_i\). ​ 请尝试对每个节点染黑或白两种颜色,并确定一个非负整数权值. ​ 问是否存在一种方案 ...

  3. 【ARC083E】Bichrome Tree 树形dp

    Description 有一颗N个节点的树,其中1号节点是整棵树的根节点,而对于第ii个点(2≤i≤N)(2≤i≤N),其父节点为PiPi 对于这棵树上每一个节点Snuke将会钦定一种颜色(黑或白), ...

  4. 【BZOJ】ARC083 E - Bichrome Tree

    [算法]树型DP [题意]给定含n个点的树的形态,和n个数字Xv,要求给每个点赋予黑色或白色和权值,满足对于每个点v,子树v中和v同色的点的权值和等于Xv.n<=10^5 [题解]首先每个点的权 ...

  5. AtCoder Regular Contest 083 E - Bichrome Tree

    题目传送门:https://arc083.contest.atcoder.jp/tasks/arc083_c 题目大意: 给定一棵树,你可以给这些点任意黑白染色,并且赋上权值,现给定一个序列\(X_i ...

  6. [AtCoder Regular Contest 083] Bichrome Tree

    树形DP. 每个点有两个属性:黑色点的权值和,白色点权值和,一个知道另一个也一定知道. 因为只要子树的和它相等的点得权值和不超过x[u],u点的权值总能将其补齐. 设计状态f[u]表示以u为根的子树, ...

  7. 【AtCoder】ARC083

    C - Sugar Water 计算一下可以达到水是多少,可以到达的糖是多少 枚举水,然后加最多能加的糖,是\(min(F - i *100,E * 100)\),计算密度,和前一个比较就行 #inc ...

  8. AtCoder Regular Contest 083

    C - Sugar Water Time limit : 3sec / Memory limit : 256MB Score : 300 points Problem Statement Snuke ...

  9. AtCoder Regular Contest 093 E: Bichrome Spanning Tree(生成树)

    Bichrome Spanning Tree 题意: 给出一个n个点,m条边的无向连通图,现在要给每条边染色,可以染成黑色或者白色. 现在要求在染色完毕后,找出一个至少包含一条黑边和一条白边的最小生成 ...

随机推荐

  1. 用valgrind检查内存问题

    Valgrind Valgrind作为一个免费且优秀的工具包,平时大部分人可能都是使用valgrind检测内存问题,如内存泄露,越界等. Valgrind工具包包含多个工具,如Memcheck,Cac ...

  2. video 的使用

    video ui给了默认的暂停图片 利用video自身的属性很难达到效果  这里自己写了个 简单记录下 <div class="cg-container video-img" ...

  3. Asp.Net Core 进阶(三)—— IServiceCollection依赖注入容器和使用Autofac替换它

    Asp.Net Core 提供了默认的依赖注入容器 IServiceCollection,它是一个轻量级的依赖注入容器,所以功能不多,只是提供了基础的一些功能,要实现AOP就有点麻烦,因此在实际工作当 ...

  4. 爬虫学习之pdf读取和存储

    在py3中如需进行pdf文件操作需要加载PDFMiner3K库文件,可通过pip方式或者可以下载源文件方式安装 python3 -m pip install pdfminer3k 下载源文件方式: 1 ...

  5. WebStorm换主题(护眼)

    一.下载喜欢颜色的主题 http://www.phpstorm-themes.com/ 我用的豆沙绿护眼 <scheme name="Solarized Light My" ...

  6. OmniFocus

    褪墨・时间管理 “把所有事情都从你的脑袋里弄出来.在事情出现就做好相关行动的一系列决定,而不是在事情爆发的时候.以合适的类别组织好你的项目的各种提醒以及下一步行动.保持你的系统更新和完整,及时进行回顾 ...

  7. 【动态规划】poj2353Ministry

    拓扑序……好些玄妙 Description Mr. F. wants to get a document be signed by a minister. A minister signs a doc ...

  8. linux定时任务执行php任务

    首先用命令检查服务是否在运行 systemctl status crond.service 如果服务器上没有装有crontab ,则可以执行 yum install vixie-cron yum in ...

  9. SVN 如何提交 SO 库文件

    今天提交代码时候发现,svn add 还是 svn st 均查看不到想要提交的 so 文件. 后来才知道原来是配置文件出了问题,把so文件的提交给屏蔽掉了. 修改步骤如下: 1.Ubuntu 系统,点 ...

  10. PHP-redis命令之 字符串 (strings)

    一.string (字符串) 1.set:设置键 $reids->set('mykey',111); 2.get:获取键 $redis->get('mykey'); 3.del:删除键 $ ...