Bichrome Tree

 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]={0},b[N]={0};
int n,x[N];

int f(int now)
{
    int len=child[now].size();
    int dp[5006]={0};
    long long sum=0;

    if(len==0)
    {
        w[now]=x[now];
        b[now]=0;
        return 1;
    }


    for(int i=0;i<len;i++)
    {
        if(f(child[now][i])==-1)return -1;
        sum+=w[child[now][i]]+b[child[now][i]];
    }

    long long now_sum=0;
    for(int i=0;i<len;i++)now_sum+=min(w[child[now][i]],b[child[now][i]]);
    if(now_sum>x[now])return -1;

    for(int i=0;i<=x[now];i++)dp[i]=i;

    for(int i=0;i<len;i++)
    {
    for(int j=x[now];j>0;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 1;
}



int main()
{
    int p[N];
    scanf("%d",&n);
    for(int i=2;i<=n;i++)
    {
        scanf("%d",&p[i]);
        child[p[i]].push_back(i);
    }
    for(int i=1;i<=n;i++)scanf("%d",&x[i]);

    if(f(1)!=-1)printf("POSSIBLE");
    else
        printf("IMPOSSIBLE");

    return 0;
}
View Code

 

 

 

转载于:https://www.cnblogs.com/tian-luo/p/9416115.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值