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 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:b
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=1e5+10;
int fa[maxn];
ll sum[maxn];
int main()
{
speed
int 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;
}
- 自定义迭代器使用foreach
- 理解cookie的path和domain属性
- 静态页面设置缓存、动态页面设缓存(不断更新中。。。。)
- 区块链技术如何把你的游戏资产真正变为你的资产
- Python标准库07 信号 (signal包,部分os包)
- 当css属性width设为100%时
- GridView实战一:自定义分页、排序、修改、插入、删除
- Windows下程序打包发布时的小技巧
- Linux的“壳”
- 网页优化系列一:合并文件请求(asp.net版)
- Windows下Thumbnail的开发总结
- 剑指OFFER之调整数组顺序使奇数位于偶数前面找(九度OJ1516)
- Linux架构
- 绘图: Python matplotlib简介
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Alink漫谈(二十一) :回归评估之源码分析
- Linux环境下通过GDB调试C项目实战
- Alink漫谈(二十二) :源码分析之聚类评估
- Python3.x将代码打包成exe程序并添加图标
- 在tinycolinux上编译pypy和hippyvm
- IDEA 热部署配置 HotSwapAgent-IntelliJ-IDEA-plugin
- 在tinycolinux上编译odoo8
- 在tinycolinux上编译jupyter和rootcling组建混合cpp,python学习环境
- 110分钟使用Python搭建自己的IP定位查询接口
- python实现在线微博数据可视化
- 在tinycolinux上安装chrome
- 如何在Ubuntu 18.04服务器上安装Python 3和设置编程环境
- python趣味题-数字加密
- 快手解析视频真实链接(爬取快手视频)
- 在tinycolinux上安装和使用cloudwall