D. Sum in the tree

题目链接:https://codeforces.com/contest/1099/problem/D

题意:

给出一棵树,以及每个点的si,这里的si代表从i号结点到根节点的权值和。但是有些si=-1,这就相当于丢失了当前结点的数据。

假设原本每个点的权值为ai,那么现在求sum{ai}的最小为多少,ai为非负数。

题解:

这题可以单独看每一条链上的s值,假设当前结点为u,儿子结点v,那么就有几种情况:

1.su==-1&&sv==-1,这种不用管,继续往下看;

2.su==-1&&sv>=0,这种情况,su以及上面可能有多个si为-1的结点,但这里我们可以就把它看作一个点,然后找到非-1父亲结点的s值,假设为p,那么sv-sp就是中间结点的权值和,看作一个点的话,就是那个点的a值,同时根据这个我们可以计算出其sum值。至于这里怎么求sp,可以在dfs的时候记录一下。

3.su>=0&&sv==-1,这里就需要上面的记录了,记录目前的su值,方便后面找sp

4.su>=0&&sv>=0,这里直接往下搜索就行了。

结合上面的分析,我们只需要一个dfs记录一下就好了,最后求au的时候就是sumu-sump,也可以在dfs的过程中处理。

但是我们刚才只是对链的分析,一个结点可能有多个儿子结点。想一下,会发现只有上面第2种情况会多考虑一点,因为根据哪个儿子结点来确定当前的s是一个问题。

这个问题也不难解决,取min{sv-sp}即可,一方面是让其尽量大,另一方面是保证方案可行。

最后再判断一下可行性就行了。

代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+;
int n;
vector <int> g[N];
ll sum[N],b[N];
int flag = ;
void dfs(int u,ll a,int fa){
int son = ;
if(flag) return ;
int f=;
for(auto v:g[u]){
if(v==fa) continue ;
if((sum[u]==- && sum[v]>=) ||f){
f=;
ll now=sum[v]-a;
if(sum[u]==-) sum[u]=now+a;
if(sum[u]!=-) sum[u]=min(sum[u],now+a);
}
}
for(auto v:g[u]){
if(v==fa) continue ;
son++;
if(sum[v]==- && sum[u]==-){
dfs(v,a,u);
continue ;
}
dfs(v,sum[u],u);
}
if(son==&&sum[u]==-){
b[u]=;
return ;
}
if(sum[u]!=-) b[u]=sum[u]-a;
}
int main(){
cin>>n;
for(int i=;i<=n;i++){
int f;
scanf("%d",&f);
g[f].push_back(i);
g[i].push_back(f);
}
for(int i=;i<=n;i++) scanf("%I64d",&sum[i]);
if(sum[]==-) sum[]=;
dfs(,,-);
for(int i=;i<=n;i++){
if(b[i]<) flag=;
}
if(flag) puts("-1");
else{
ll ans = ;
for(int i=;i<=n;i++) ans+=b[i];
cout<<ans;
}
return ;
}
/*
7
1
2
1
4 4 5
0 -1 3 -1 -1 3 -1
*/

Codeforces Round #530 (Div. 2):D. Sum in the tree (题解)的更多相关文章

  1. Codeforces Round #530 (Div. 2) D. Sum in the tree 树上贪心

    D. Sum in the tree 题意 给出一颗树,奇数层数的点有值,值代表从1到该点的简单路的权值的和,偶数层数的点权值被擦去了 问所有节点的和的最小可能是多少 思路 对于每一个-1(也就是值未 ...

  2. Codeforces Round #530 (Div. 1) 1098A Sum in the tree

    A. Sum in the tree Mitya has a rooted tree with nn vertices indexed from 11 to nn, where the root ha ...

  3. Codeforces 1099 D. Sum in the tree-构造最小点权和有根树 贪心+DFS(Codeforces Round #530 (Div. 2))

    D. Sum in the tree time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. Codeforces Round #530 (Div. 2) A,B,C,D

    A. Snowball 链接:http://codeforces.com/contest/1099/problem/A 思路:模拟 代码: #include<bits/stdc++.h> ...

  5. Codeforces Round #530 (Div. 2)

    RANK :2252 题数 :3 补题: D - Sum in the tree 思路:贪心 把权值放在祖先节点上 ,预处理 每个节点保存 他与他儿子中 权值最小值即可. 最后会有一些叶子节点依旧为 ...

  6. Codeforces Round #530 (Div. 1)

    A - Sum in the tree 就是贪心选尽量让上面的点权尽量大,那么对于偶数层的点,其到根节点的和即为所有儿子中的最大值. #include<bits/stdc++.h> usi ...

  7. Codeforces Round #530 (Div. 2) Solution

    A. Snowball 签. #include <bits/stdc++.h> using namespace std; ], d[]; int main() { while (scanf ...

  8. Codeforces Round #530 (Div. 2) F (树形dp+线段树)

    F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...

  9. Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)

    https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...

随机推荐

  1. python 终极篇 ---- 中间件

    中间件------------------------>>>>>>>>>>>>> 中间件是一个用来处理django的响应与 ...

  2. python 终级篇 django ---ORM操作

                                       一般操作                                                          必会的 ...

  3. Paper Reading - Show, Attend and Tell: Neural Image Caption Generation with Visual Attention ( ICML 2015 )

    Link of the Paper: https://arxiv.org/pdf/1502.03044.pdf Main Points: Encoder-Decoder Framework: Enco ...

  4. 【Paper】Deep & Cross Network for Ad Click Predictions

    目录 背景 相关工作 主要贡献 核心思想 Embedding和Stacking层 交叉网络(Cross Network) 深度网络(Deep Network) 组合层(Combination Laye ...

  5. axis2调用webService几种方式

    主要有三种方式: 第一RPC方式,不生成客户端代码 第二,document方式,不生成客户端代码 第三,用wsdl2java工具,生成客户端方式调用 java代码: package samples.q ...

  6. HDU 3264/POJ 3831 Open-air shopping malls(计算几何+二分)(2009 Asia Ningbo Regional)

    Description The city of M is a famous shopping city and its open-air shopping malls are extremely at ...

  7. Code obfuscatio (翻译!)

    Description Kostya likes Codeforces contests very much. However, he is very disappointed that his so ...

  8. 20162328蔡文琛week02

    学号 20162328 <程序设计与数据结构>第2周学习总结 教材学习内容总结 这周学习了课本中的第二章内容,比起第一章,本章难度有略微底稿,从刚开始的显示字符转变为简单的加减乘除运算,经 ...

  9. Android - 时间 日期相关组件

    源码下载地址 : -- CSDN :  http://download.csdn.net/detail/han1202012/6856737 -- GitHub : https://github.co ...

  10. Java中I/O流之Object流

    Java 中的 object 流:直接将 Object 对象写入或读出 1. serializable 接口:序列化,可以被序列化的,若确实需要将某个类的对象写在硬盘上或网络上,想把他们序列化成一个字 ...