UPC 6611 Bichrome Tree

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Seeyouer/article/details/81382996

题目描述

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.

如下图,粉色为x[i],蓝色为(f[i],x[i])。f[i]代表 以i为跟的所有的 与i结点颜色不同的 子节点 的权值和。
显然,对每个节点,咱们应该尽可能的用子节点的权值将本结点的x[i]填满,这就成了背包问题了。在这里,我们对结点i 做dp的时候,结点i的每个孩子结点j(不包含孙子结点)中,必须在f[j]和x[j]中选择一个,因为如果i和j不同色,那i就必须选择与j不同色的 j的子节点 的权值和,即f[j];否则就选择x[j]。

这里写图片描述

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,p;
struct edge{
    ll en,next;ll cost;
}e[300010];
int w[1010],x[1010];
int head[300010],cnt=0,vis[200010]={0};
void add(ll start,ll ennd,ll c){//头插法,单向
    e[cnt].en=ennd;
    e[cnt].next=head[start];
    e[cnt].cost=c;
    head[start]=cnt;//记录下标
    cnt++;
}
int flag=1,dp[5050][5050],f[5050],W=0,b=0;
int dfs(int u){
    if(vis[u]||flag==0) return 0;
    vis[u]=1;
    f[u]=0x3f3f3f3f;
    memset(dp[u],0x3f3f3f3f,sizeof(dp[u]));
    dp[u][0]=0;
    if(head[u]==-1) {f[u]=0;return 0;}
    for(int i=head[u];i!=-1;i=e[i].next){
        dfs(e[i].en);
        for(int j=x[u];j>=0;j--){
            int temp=0x3f3f3f3f;
            if(j-x[e[i].en]>=0) temp=min(temp,dp[u][j-x[e[i].en]]+f[e[i].en]);
            if(j-f[e[i].en]>=0) temp=min(temp,dp[u][j-f[e[i].en]]+x[e[i].en]);
            dp[u][j]=temp;
        }
    }
    for(int i=0;i<=x[u];i++)
        f[u]=min(f[u],dp[u][i]);
    if(f[u]==0x3f3f3f3f){ flag=0;return 0;}
}

int main(){
    scanf("%d",&n);
    for(int i=0;i<=n;i++){
        head[i]=-1;
    }
    for(int i=2;i<=n;i++){
        scanf("%d",&p);
        add(p,i,1);
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x[i]);
    }
    dfs(1);
    if(flag){
        printf("POSSIBLE\n");
    }
    else{
        printf("IMPOSSIBLE\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Seeyouer/article/details/81382996
UPC