UPC 6611 Bichrome Tree(树状DP)

题目描述

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.

题意
给一个n个节点的树,满足第i节点的权值和他所有子节点中与他颜色相同的节点权值相加等于xi,问是否存在这样的树。

思路
要尽量保留值较小的颜色与i的颜色不同,以满足还未确定的节点的x;因为求第i个点时,以它为根的子节点的x值是确定的,即总值Ans确定了;到i点时,一定会有一种颜色的值要变为xi,那么肯定要尽可能大的取Ans中值,保留小的值给它上面的点留下机会。到i时,它的每个子节点的总值由两种颜色的值组成,令值大的为high,小的为low,要是所有的low加起来还大于xi,则不可能,否则尽可能地取high与i颜色一样

#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct fun{
    int low,high;
    int lh;
}a[1005];
vector<int>v[1005];
const int inf=10000009;
int flog=0;
int dp[5000005],x[1005];
int dfs(int u)
{
    int n=v[u].size();
    if(n)
    {
        int sum=0,Ans=0;//sum为子节点low的和,Ans为总和
        for(int i=0;i<n;i++)
        sum+=dfs(v[u][i]),Ans+=a[v[u][i]].high+a[v[u][i]].high;
        if(sum>x[u])
        {
            flog=1;//不可能有这样的树
            return inf;
        }
        //01背包尽可能地取high
        int m=x[u]-sum;
        for(int i=0;i<=m;i++)
        dp[i]=0;
        for(int i=0;i<n;i++)
         {
            int xx=v[u][i];
            for(int j=m;j>=a[xx].lh;j--)
           dp[j]=max(dp[j],dp[j-a[xx].lh]+a[xx].lh);
         }
         int Max=0;
         for(int i=0;i<=m;i++)
         Max=max(Max,dp[i]);
         int t=sum+Max;
         Ans-=t;
         a[u].high=max(Ans,x[u]);//到第u个节点时,值大的颜色,low相反
         a[u].low=min(Ans,x[u]);
         a[u].lh=a[u].high-a[u].low;
    }
    return a[u].low;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=2;i<=n;i++)
    {
        int xx;
        scanf("%d",&xx);
        v[xx].push_back(i);
    }
    for(int i=1;i<=n;i++)
    scanf("%d",&x[i]);
    for(int i=1;i<=n;i++)
    if(v[i].size()==0)//叶子节点
    {
        a[i].high=a[i].lh=x[i];
        a[i].low=0;
    }
    dfs(1);
    if(flog)
    printf("IMPOSSIBLE\n");
    else
    printf("POSSIBLE\n");
    return 0; 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值