问题 K: Bichrome Tree

问题 K: Bichrome Tree

时间限制: 1 Sec  内存限制: 128 MB
提交: 196  解决: 55
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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.

 

转载自这儿、

dp-------,我dp真是差的一批

蓝色为我自己的理解

【题意】

给出一棵树,你可以给每个结点分配一个颜色(黑或白)和一个数值(任意数字)。

给出一个序列Xi。

问是否能构造出这样的情况:

对于任意的点u,点u和他的子树中相同颜色的结点上,所分配数值之和,恰好等于Xu

【分析】

黑白颠倒无所谓。我们可以让结点尽量是黑色,那么对于结点u,它子树中黑色结点之和就是Xu,设白色之和为f[u]。

尽量要求f[u]小,就能使得白色的祖先可以承受下u子树的白色们(白色之和太大,就会超过Xi,从而使得白色祖先无法撑得下白色)。

那么对于点u,任意的子树v,我可以令v是黑色(与u同色),则Xv要算进u子树,我也可以令v是白色(与u不同色),则f[v]要算进u子树。

 

从这里开始往下,不再纠结于黑白色,而是将关注点放在同色不同色上

f[u]就是u的子树中 与u 不同色的结点之和,我们让这个值尽量小,就能最大可能的满足u的祖先的需求。

对每一个点u做所有孩子的背包就好。

点u的状态有两种取值:

1:u与v同色,把Xv装进u结点, dp[j] = min( dp[j-X[v]]+f[v] );

2: u与不同色,把f[v]装进u结点,dp[j] = min( dp[j-f[v]]+X[v] );

最后如果f[1]为无穷,说明无论如何,1号点都无法容纳与其不同色的点之和。

 

 

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int MAX=2e5+5;
struct Stela{
    int from,to;
}Stla[MAX];
int head[MAX],cnt;
void add(int from,int to){
    Stla[cnt].from = to,
    Stla[cnt].to = head[from];
    head[from] = cnt++;
}
int f[MAX],dp[5005],P,X[MAX],n;

void dfs(int u)
{
    for(int e = head[u];~e;e=Stla[e].to)dfs(Stla[e].from);
    memset(dp,0,sizeof(dp[0]*(X[u]+1)));
    dp[0]=0;

    for(int e = head[u];~e;e=Stla[e].to)
    {
        int v = Stla[e].from;

        for(int i = X[u];i>=0;i--)
        {
            int tdp = inf;
            if(i>=X[v])tdp=min(tdp,dp[i-X[v]]+f[v]);
            if(i>=f[v])tdp=min(tdp,dp[i-f[v]]+X[v]);
            dp[i]=tdp;
        }
    }
    for(int i=0;i<=X[u];i++)f[u]=min(f[u],dp[i]);
}
int main()
{
    scanf("%d",&n);

    cnt = 0,memset(head,-1,sizeof(head[0])*(n+1));

    for(int i=2;i<=n;i++)scanf("%d",&P),add(P,i);

    for(int i=1;i<=n;i++)scanf("%d",&X[i]);

    memset(f,0x3f,sizeof(f));

    dfs(1);

    if(f[1]<inf)puts("POSSIBLE");
    else  puts("IMPOSSIBLE");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值