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. SQL增删查改语句

    一.增:有4种方法 1.使用insert插入单行数据: 语法:insert [into] <表名> [列名] values <列值> insert into sheet1 va ...

  2. confluent 更换ip地址之后修改数据库

    由于地址搬迁导致ip地址有变动,整个的confluence的服务器的ip的地址更换和对应的数据库地址更换 root@computer-PowerEdge-T30:/opt/atlassian/conf ...

  3. vector的基本用法

    #include<iostream> #include<vector> #include<algorithm> using namespace std; int m ...

  4. C#背景图片自适应

    1.选中窗体修改属性 2.在load添加代码 private void Form1_Load(object sender, EventArgs e) { this.BackgroundImageLay ...

  5. vue 动态合并单元格、并添加小计合计功能

    1.效果图 2.后台返回数据格式(平铺式) 3.后台返回数据后,整理所需要展示的属性存储到(items)数组内 var obj = { "id": curItems[i].id, ...

  6. jExcelAPI导入导出excel

      MS的电子表格(Excel)是Office的重要成员,是保存统计数据的一种常用格式.作为办公文档,势必要涉及到的电子文档的交换,Excel是一种在企业中非常通用的文件格式,打印和管理也比较方便.在 ...

  7. Shell脚本调用SQL文格式

    Shell脚本调用SQL文格式 1. 定义需要执行的SQL文,以及需要输出文件 OUTFILE=\${DATADIR}/\${FILENAME} SQLFILE=\${DATADIR}/check_t ...

  8. UVa-156-反片语

    这题比较精妙的是,我们对于单词重排,实际上是进行了标准化的处理,即按照字典序排序. 这样的话,就很方便地处理了单词的重排问题,我们不需要使用全排列函数进行排列尝试,我们直接化简为一,然后进行比较就可以 ...

  9. CSS3-文本-word-wrap,word-break,white-space

    一.word-wrap使用: 语法: word-wrap : normal | break-word 取值说明: 1.normal为默认值,当其值为normal控制连续文本换行(允许内容顶开容器的边界 ...

  10. 【linux】【git】安装/升级Git 1.9.4

      因为yum源的最新版本是1.7.x,所以无法通过yum进行更新,下面描述如何通过编译源码进行安装 1.安装需要的依赖 第一步我们需要做的就是确认系统已经安装了编译git时需要的依赖.使用下面的安装 ...