[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;
}

 

转载于:https://www.cnblogs.com/xfl03/p/9396670.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值