A. Sum in the tree

Mitya has a rooted tree with nn vertices indexed from 11 to nn, where the root has index 11. Each vertex vv initially had an integer number av≥0av≥0 written on it. For every vertex vv Mitya has computed svsv: the sum of all values written on the vertices on the path from vertex vv to the root, as well as hvhv — the depth of vertex vv, which denotes the number of vertices on the path from vertex vv to the root. Clearly, s1=a1s1=a1 and h1=1h1=1.

Then Mitya erased all numbers avav, and by accident he also erased all values svsv for vertices with even depth (vertices with even hvhv). Your task is to restore the values avav for every vertex, or determine that Mitya made a mistake. In case there are multiple ways to restore the values, you’re required to find one which minimizes the total sum of values avav for all vertices in the tree.

Input
The first line contains one integer nn — the number of vertices in the tree (2≤n≤1052≤n≤105). The following line contains integers p2p2, p3p3, … pnpn, where pipi stands for the parent of vertex with index ii in the tree (1≤pi<i1≤pi<i). The last line contains integer values s1s1, s2s2, …, snsn (−1≤sv≤109−1≤sv≤109), where erased values are replaced by −1−1.

Output
Output one integer — the minimum total sum of all values avav in the original tree, or −1−1 if such tree does not exist.

Examples
input
5
1 1 1 1
1 -1 -1 -1 -1
output
1
input
5
1 2 3 1
1 -1 2 -1 -1
output
2
input

3
1 2
2 -1 1
output
-1

这道题时说有一颗树,树有N个节点,每个节点有一个值Vi,沿着根节点到I节点求和得到SUMi,前缀和,现在删去vi,且删去了偶数深度节点的sumi,vi>=0,sumi<=1e9.现是否存在一棵这样的树使得各点vi的和相加最小,不存在就是,存在子节点sumi小于父节点sumi使得子节点vi为负值。当为偶数节点时,他要被做差分,他也要与父节点做差分,如果他有子节点时,他们几个构成贪心的机制,代价=∑(sum[子节点]-sum[当前节点])+sum[当前节点]-sum[父节点] 可知当子节点大于1的时候sum[当前]越大越好,最大的符合条件的sum[当前]是 sum【子节点】的最小值,当没有子节点时,显而易见等于父节点的sum【父节点】时代价最小,当只有一个子节点时,代价去sum【子】与sum【父】带价相同,归到第一类,可以写代码了。

    #include<bits/stdc++.h>#define Swap(a,b) a^=b^=a^=b#define cini(n) scanf("%d",&n)#define cinl(n) scanf("%lld",&n)#define cinc(n) scanf("%c",&n)#define speed ios_base::sync_with_stdio(0); // 切不可用scnaf;#define Max(a,b) a>b?a:b#define Min(a,b) a<b?a:busing namespace std;typedef long long ll;const int INF=0x3f3f3f3f;const int maxn=1e5+10;int fa[maxn];ll sum[maxn];int main(){speedint n;ll ans=0;cin>>n;for(int i=2;i<=n;i++){cin>>fa[i];}for(int i=1;i<=n;i++){cin>>sum[i];if(sum[i]==-1) sum[i]=1e9+1;// sum[i]=sum[i]==-1?1e9+1:sum[i];}// for(int i=1;i<=n;i++) cout<<sum[i]<<' '<<endl;// cout<<endl;for(int i=2;i<=n;i++){sum[fa[i]]=Min(sum[fa[i]],sum[i]);}//    for(int i=1;i<=n;i++) cout<<sum[i]<<' '<<endl;for(int i=2;i<=n;i++){int temp=sum[i]-sum[fa[i]];if(temp<0) return cout<<-1<<endl,0;if(sum[i]<=1e9) ans+=temp;}cout<<sum[1]+ans<<endl;}

Codeforces Round #530 (Div. 1) 1098A Sum in the tree相关推荐

  1. Codeforces Round #599 (Div. 2) E. Sum Balance 图转换 + 子集dp + 环

    传送门 文章目录 题意: 思路: 题意: 思路: 首先我们知道如果所有数的和summodk!=0sum\bmod k!=0summodk!=0那么此时无解,否则我们设need=sum/kneed=su ...

  2. Codeforces Round #624 (Div. 3) E. Construct the Binary Tree 思维 + 构造

    传送门 文章目录 题意: 思路: 题意: 给你n,dn,dn,d,让你构造有nnn个点的二叉树,他们每个节点深度和为ddd. n,d≤3000n,d\le 3000n,d≤3000. 思路: 先考虑不 ...

  3. Codeforces Round #530 (Div. 2)

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

  4. Codeforces Round #530 (Div. 1) C. Construct a tree 想法

    题目链接:http://codeforces.com/contest/1098/problem/C 题意: 给你n个点要你组成一棵以结点1为根的树,使得所有结点的子树的大小和为s,且在这种情况下使得分 ...

  5. Codeforces Round #530 Div. 1 自闭记

    A:显然应该让未确定的大小尽量大.不知道写了啥就wa了一发. #include<iostream> #include<cstdio> #include<cmath> ...

  6. Codeforces Round #702 (Div. 3)---C. Sum of Cubes 两种方法 cbrt()函数应用

    文章目录 题目 思路 代码 题目 题目链接 思路 思路一. 容易联想到枚举所有x存在的情况,用map映射.因为a取值在1e4上. 1e8的时间复杂度枚举,但是map具有自动排序 费时间,炸了. uno ...

  7. Codeforces Round #702 (Div. 3) C. Sum of Cubes

    题意: 你得到一个正整数 x.检查数字 x 是否可以表示为两个正整数的立方和. 正式地,您需要检查是否有两个整数 a 和 b (1≤a,b) 使得 a^3+b^3=x. 例如,如果 x=35,则数字 ...

  8. Codeforces Round #383 (Div. 1): D. Arpa’s letter-marked tree…(dsu on tree+状压)

    题意: 给你一棵n个节点的树,每条边都代表着一个字母(a~v),对于每个节点u,求出以u为根的子树中有多少条路径满足:路径上的字符重新排列后可以得到一个回文字符串 思路: 前置:dsu on tree ...

  9. Codeforces Round #636 (Div. 3) D. Constant Palindrome Sum 思维 + 差分

    传送门 文章目录 题意: 思路: 题意: 思路: 首先有一个显然的性质就是每组操作最多不会超过两次. 很容易想到一个很暴力的思路,就是枚举x∈[1,2∗k]x \in [1,2*k]x∈[1,2∗k] ...

最新文章

  1. 《深入理解Java虚拟机》-----第3章 垃圾收集器与内存分配策略
  2. 【数据结构作业心得】纸面6 - Matlab LU分解
  3. springboot+vue前后端分离实现宿舍管理系统
  4. 【Hibernate步步为营】--关联映射之多对一
  5. OAuth 2.0协议在SAP产品中的应用
  6. C++服务器设计(七):聊天系统服务端实现
  7. 谈谈工业界落地能力最强的机器学习算法
  8. 人工智能撰稿将代替人工写作?我们拭目以待
  9. 署五笔软件测试初学者,三天学会五笔打字练习测试新手教程
  10. 曾宪武《物联网通信技术》课后答案(二)
  11. WCF学习记录【一】
  12. R语言ggplot2绘图
  13. 仿ios相机apk_icamera仿苹果安卓版-icamera仿苹果软件下载-多多root网
  14. python-函数参数和文档
  15. redis持久化--AOF(九)
  16. java写 狐狸找兔子_狐狸找兔 算法分析
  17. win10网络邻居看到linux,win10网络邻居找不到其他电脑怎么办
  18. 动态生成验证码+验证码的校验
  19. 基于Matlab的脑功能网络工具箱 BCT FCLab
  20. 红米K40参数和配置 红米k40上市时间和价格

热门文章

  1. linux内核模块配置,如何为linux内核模块开发配置clang_complete?
  2. 组件化开发实战_一篇文章搞懂什么是前端“组件化”开发
  3. APNS提供了两项基本的服务:消息推送和反馈服务
  4. 4种实例 advice aop_《Spring实战》阅读笔记
  5. PyCharm入门教程——在编辑器中选择文本
  6. Flume学习_接收器
  7. 切图网——好的网站结构有利于seo
  8. RHEL5下的DHCP服务和DHCP中继
  9. WebSen!NT的行业分类说明
  10. 6月第2周回顾:雅虎收购案谈崩 中国***成焦点