zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    博客园电子期刊2009年6月刊发布
    今晚22:30~23:00博客程序更新
    博客园上海俱乐部活动通知(2009613)
    【意见征集补充】09'博客园T恤设计
    C# WinForm webBrowser 内嵌网页的按钮的OnClientClick事件的return false 在webBrowser中绑定onclick事件后 失效 的变通解决办法
    [转]VS2008中开发智能设备程序的一些总结收藏1
    [转]C#访问SQLite数据库
    [转]VS C# 怎么调试调试服务?
    [转]弹出窗口刷新它的父页面后。出现不重新发送信息,则无法刷新网页
    [转]外部css文件中的 BACKGROUNDIMAGE: url(..\image.gif)指定的背景图像无法显示,谁有好主意?
  • 原文地址:https://www.cnblogs.com/tian-luo/p/9416115.html
Copyright © 2011-2022 走看看