• [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;
    }
  • 相关阅读:
    python实现git操作
    CentOS 安装R包 报错:internet routines cannot be loaded ,如何解决
    CentOS 6.8上安装指定版本的注释软件 VEP,release 93.4
    Anaconda3安装后在Pycharm中报错:ImportError: DLL load failed: 找不到指定的模块
    CentOS编译fastp 0.19.4 时报错/usr/bin/ld: cannot find -lz
    推荐一款非常好用的在线python编辑器——Colaboratory
    mac如何用quick look预览多个文件或者图片
    Pileup 格式详细说明
    CentOS 7 上安装 Django 2.2.4,解决报错:No module named ‘_sqlite3′
    Message: 'chromedriver' executable needs to be available in the path.
  • 原文地址:https://www.cnblogs.com/xfl03/p/9396670.html
Copyright © 2020-2023  润新知