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. CF 1119F Niyaz and Small Degrees

    打VP的时候由于CXR和XRY切题太快了导致我只能去写后面的题了 然而VP的时候大概还有一小时时想出了\(O(n^2\log n)\)的暴力,然后过了二十分钟才想到删点的优化 结果细节很多当然是写不出 ...

  2. 手把手教你写 Vue UI 组件库

    最近在研究 muse-ui 的实现,发现网上很少有关于 vue 插件具体实现的文章,官方的文档也只是一笔带过,对于新手来说并不算友好. 笔者结合官方文档,与自己的摸索总结,以最简单的 FlexBox  ...

  3. DaemonSet 案例分析

    本节详细分析两个 k8s 自己的 DaemonSet:kube-flannel-ds 和 kube-proxy . kube-flannel-ds 下面我们通过分析 kube-flannel-ds 来 ...

  4. CAD交互绘制圆形批注(网页版)

    js中实现代码说明: 动态拖放时的绘制事件: function DoDynWorldDrawFun(dX,dY,pWorldDraw,pData) { //自定义实体的GUID标识符 var sGui ...

  5. 工程化---cnpm不是内部命令的解决

    (1)问题描述 安装完,执行cnpm -v发现报出不是内部命令. 安装成功如下图: (2)解决方案: 之前配置过默认安装都会在D:\\nodejs\node_global中,所有我们cd 到 这个路径 ...

  6. monkeyrunner之控件ID不存在或重复(转载lynnLi)

    我们在用monkeyrunner进行Android自动化时,通过获取坐标点或控件ID进行一系列操作.由于使用坐标点时,屏幕分辨率一旦更改,则代码中用到坐标的地方都要修改,这样导致代码的复用率较低.因此 ...

  7. PAT (Basic Level) Practise (中文)-1030. 完美数列(25)

    PAT (Basic Level) Practise (中文)-1030. 完美数列(25)   http://www.patest.cn/contests/pat-b-practise/1030 给 ...

  8. bootstrap历练实例:标签式的导航菜单

    本章将讲解bootstrap提供的用于定义导航元素的一些选项,它使用相同的标签和基类.nav.Bootsrtap也提供了一个用于共享标记和状态的帮助器类.改变修饰的class,可以在不同的样式间进行切 ...

  9. 使用 ss 命令查看连接信息

    作用:打印主机socket连接信息,netstate可以做的它都可以做,比netstate 更灵活,而且由于ss使用 tcp_diag 内核模块,所以速度更快. 用法: ss [ OPTIONS ] ...

  10. Linux下关于/tmp目录的清理规则

    本文将介绍Linux下/tmp目录的清理规则,rhel6和rhel7将以完全不同的两种方式进行清理. RHEL6 tmpwatch命令 tmpwatch 是专门用于解决“删除 xxx天没有被访问/修改 ...