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. 验证 .NET 4.6 的 SIMD 硬件加速支持的重要性

    SIMD 的意思是 Single Instruction Multiple Data.顾名思义,一个指令可以处理多个数据. .NET Framework 4.6 推出的 Nuget 程序包 Syste ...

  2. 基于Python的Web应用开发实战——3 模板

    要想开发出易于维护的程序,关键在于编写形式简洁且结构良好的代码. 当目前为止,你看到的示例都太简单,无法说明这一点,但Flask视图函数的两个完全独立的作用却被融合在了一起,这就产生了一个问题. 视图 ...

  3. vscode 插件整理

    己亥年  庚午月 癸巳日  宜入宅 忌婚嫁 1.Chinese (Simplified) Language Pack for Visual Studio Code 此中文(简体)语言包为 VS Cod ...

  4. Bootstrap历练实例:表单控件状态(禁用的字段集fieldset)

    禁用的字段集 fieldset 对 <fieldset> 添加 disabled 属性来禁用 <fieldset> 内的所有控件. <!DOCTYPE html>& ...

  5. 关于Java IO流学习总结

    一.IO流的三种分类方式 1.按流的方向分为:输入流和输出流 2.按流的数据单位不同分为:字节流和字符流 3.按流的功能不同分为:节点流和处理流     二.IO流的四大抽象类: 字符流:Reader ...

  6. linux的less命令

    less 在查看之前不会加载整个文件.可以尝试使用 less 和 vi 打开一个很大的文件,你就会看到它们之间在速度上的区别. 在 less 中导航命令类似于 vi.本文中将介绍一些导航命令以及使用 ...

  7. PAT 乙级 1011

    题目 题目地址:PAT 乙级 1011 思路 这道题的比较坑的地方在于给定数据的范围 int 类型的数据大小是[-2^31 , 2^31 -1] 即 [-2147483648,2147483647] ...

  8. Re:从零开始的Linux之路(文件权限)

    基于 Red Hat Enterprise Linux 7.5 或者 CentOS 7.4 基本概念 Linux最核心的一个概念就是:Linux里面任何东西都可以被视为一个文件,包括系统本身(说到底L ...

  9. js 字符串加密

    加密: 1.获得要加密的字符串:var str=input.value; 2.转化: for(var i=0;i<str.length;i++){ str+=String.fromCharCod ...

  10. C++代码学习之一:组合模式例子

    #include"AbstractFile.h" void AbstractFile::add(AbstractFile*) { } void AbstractFile::remo ...