upc 6611: Bichrome Tree (树上dp)

6611: Bichrome Tree

时间限制: 1 Sec  内存限制: 128 MB
提交: 132  解决: 36
[提交] [状态] [讨论版] [命题人: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.

【小结】

我好菜

【题意】

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

给出一个序列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号点都无法容纳与其不同色的点之和。

【代码】

/****
***author: winter2121
****/
#include<bits/stdc++.h>
#define SI(i) scanf("%d",&i)
#define PI(i) printf("%d\n",i)
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int MAX=2e5+5;
const int INF=0x3f3f3f3f;
int dir[9][2]={0,1,0,-1,1,0,-1,0, -1,-1,-1,1,1,-1,1,1};
template<class T>bool gmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>bool gmin(T &a,T b){return a>b?a=b,1:0;}

ll gcd(ll a,ll b){ while(b) b^=a^=b^=a%=b; return a;}
ll inv(ll b){return b==1?1:(mod-mod/b)*inv(mod%b)%mod;}
ll qpow(ll n,ll m)
{
    n%=mod;
    ll ans=1;
    while(m)
    {
        if(m%2)ans=(ans*n)%mod;
        m>>=1; n=(n*n)%mod;
    }
    return ans;
}

struct node{
    int t,next;
}edge[MAX];
int head[MAX],cnt;
void initedge(int n)
{
    cnt=0; memset(head,-1,sizeof(head[0])*(n+1));
}
void addedge(int u,int v)
{
    edge[cnt]=node{v,head[u]};
    head[u]=cnt++;
}

int f[MAX],dp[5050],P[MAX],X[MAX],n;
int dfs(int u)
{
    for(int i=head[u];~i;i=edge[i].next)dfs(edge[i].t); //注意一定要先dfs所有子树,因为dp数组公用,不能一边dfs让子树用dp,否则dp数组总是被子节点更改
    memset(dp,0x3f,sizeof(dp[0])*(X[u]+1));
    dp[0]=0;
    for(int i=head[u];~i;i=edge[i].next)
    {
        int v=edge[i].t;
        for(int j=X[u];j>=0;j--)
        {
            int temp=INF;
            if(j>=X[v])gmin(temp,dp[j-X[v]]+f[v]); //与子节点同色
            if(j>=f[v])gmin(temp,dp[j-f[v]]+X[v]); //不同色,即颜色颠倒一下
            dp[j]=temp;
        }
    }
    for(int j=0;j<=X[u];j++)gmin(f[u],dp[j]);
}
int main()
{
    SI(n);
    initedge(n);
    for(int i=2;i<=n;i++)SI(P[i]),addedge(P[i],i);
    for(int i=1;i<=n;i++)SI(X[i]);
    memset(f,0x3f,sizeof(f));
    dfs(1);
    if(f[1]<INF)puts("POSSIBLE");
    else  puts("IMPOSSIBLE");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/winter2121/article/details/81326928