zoukankan      html  css  js  c++  java
  • [AtCoder][ARC083]Bichrome Tree 题解

    Bichrome Tree

    时间限制: 1 Sec 内存限制: 128 MB

    原题链接 https://arc083.contest.atcoder.jp/tasks/arc083_c

    题目描述

    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: 

    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.

    样例输入


    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.

    题解

    给予所有节点一个颜色和权值,使得节点v和其所有同色子节点权值和为X[v]。 
    这道题的关键点在于具体什么颜色不影响最后的结果,而和子节点颜色的关系影响结果,所以可以把关注点放在如何分配权值而不是如何分配颜色上。 
    我的做法是进行一次dp,不过我的dp操作感觉不是特别优雅,理论上应该能找到一条dp方程来等效我的操作的QAQ

    代码

    #include <iostream>
    
    using namespace std;
    int n, p[1007], x[1007], dp[1007][5007], t, fa, lim, odp;
    
    int main() {
        ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    
        cin >> n;
        for (int i = 2; i <= n; i++) cin >> p[i];
        for (int i = 1; i <= n; i++) cin >> x[i];
    
        for (int i = n; i > 0; i--) {
            fa = p[i], lim = x[i];
            for (int j = x[fa]; j >= 0; j--) {
                odp = dp[fa][j], dp[fa][j] = (int) 1e5;
                if (lim <= j)
                    dp[fa][j] = (lim == 0 ? odp : dp[fa][j - lim]) + dp[i][lim];
    
                if (dp[i][lim] <= j) {
                    t = (dp[i][lim] == 0 ? odp : dp[fa][j - dp[i][lim]]) + lim;
                    if (dp[fa][j] > t) dp[fa][j] = t;
                }
            }
        }
        cout << (dp[1][x[1]] < (int) 1e5 ? "POSSIBLE" : "IMPOSSIBLE");
        return 0;
    }
  • 相关阅读:
    jquery中子元素和后代元素的区别
    MVC4 创建控制器时,无法检索元数据
    ref和out的用法说明举例(转)
    DataView的ToTable方法,类似数据库Distinct。
    oracle 19C 静默安装(单机版)
    Supervisord进程管家
    zabbix v3.0安装部署【转】
    Linux下Hadoop2.7.3集群环境的搭建
    Redis集群搭建与简单使用【转】
    redis+keepalived实现高可用
  • 原文地址:https://www.cnblogs.com/xfl03/p/9396670.html
Copyright © 2011-2022 走看看