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;
}
Codeforces Round #530 (Div. 1) 1098A Sum in the tree的更多相关文章
- Codeforces Round #530 (Div. 2):D. Sum in the tree (题解)
D. Sum in the tree 题目链接:https://codeforces.com/contest/1099/problem/D 题意: 给出一棵树,以及每个点的si,这里的si代表从i号结 ...
- Codeforces Round #530 (Div. 2) D. Sum in the tree 树上贪心
D. Sum in the tree 题意 给出一颗树,奇数层数的点有值,值代表从1到该点的简单路的权值的和,偶数层数的点权值被擦去了 问所有节点的和的最小可能是多少 思路 对于每一个-1(也就是值未 ...
- 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 ...
- Codeforces Round #530 (Div. 2) A,B,C,D
A. Snowball 链接:http://codeforces.com/contest/1099/problem/A 思路:模拟 代码: #include<bits/stdc++.h> ...
- Codeforces Round #530 (Div. 2)
RANK :2252 题数 :3 补题: D - Sum in the tree 思路:贪心 把权值放在祖先节点上 ,预处理 每个节点保存 他与他儿子中 权值最小值即可. 最后会有一些叶子节点依旧为 ...
- Codeforces Round #530 (Div. 1)
A - Sum in the tree 就是贪心选尽量让上面的点权尽量大,那么对于偶数层的点,其到根节点的和即为所有儿子中的最大值. #include<bits/stdc++.h> usi ...
- Codeforces Round #530 (Div. 2) Solution
A. Snowball 签. #include <bits/stdc++.h> using namespace std; ], d[]; int main() { while (scanf ...
- Codeforces Round #530 (Div. 2) F (树形dp+线段树)
F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...
- Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)
https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...
随机推荐
- 合理使用CSS框架,加速UI设计进程
转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文出处:https://dzone.com/articles/how-to-speed-up-your-d ...
- Android传感器--光照传感器使用
Android 设备中有许多传感器,其中有一个传感器控制着你屏幕亮度的变化.当你在很暗的地方使用手机,你设备的屏幕会自动调暗,从而保护你眼睛. 起着这样作用,Android是通过一款光照传感器来获取你 ...
- .net批量更新(插入、修改、删除)数据库
思路: 1. 设置DataTable中每行的状态标识,即调用DataRow的方法setAdded().setModified().Delete() 2. 使用DataAdapter的Update(Da ...
- Python—一个简单搜索引擎索引库
因为课业要求,搭建一个简单的搜索引擎,找了一些相关资料并进行了部分优化(坑有点多) 一.数据 数据是网络上爬取的旅游相关的攻略页面 这个是travels表,在索引中主要用到id和url两个字段. 页面 ...
- 接口测试中实际发生的几个问题——python中token传递
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:AFKplayer PS:如有需要Python学习资料的小伙伴可以加点 ...
- Sprint 2 : ios图形界面设计与代码整合
这周我们主要focus在personal photo experience 项目的ios图形界面设计与代码整合工作上. 工作进度: 1. 图形界面设计方面:兆阳和敏龙基本已经将ios手机客户端的雏形界 ...
- copy模块中的copy与deepcopy的区别
前言 每空闲下来,就觉得以前写的博客很low........也许现在也很low~~~~好吧就当升级版的low吧~~~~ 如果要了解copy与deepcopy的区别,就需要了解Python的存储机制:P ...
- Python之疑难杂症包安装
ansible 直接用pip install 安装一直失败 1.下载ansible压缩包 https://files.pythonhosted.org/packages/ec/ee/1494474b5 ...
- 词向量模型word2vector详解
目录 前言 1.背景知识 1.1.词向量 1.2.one-hot模型 1.3.word2vec模型 1.3.1.单个单词到单个单词的例子 1.3.2.单个单词到单个单词的推导 2.CBOW模型 3.s ...
- 详解 方法的覆盖 —— toString() 与 equals()的覆盖
在学习本篇博文前,建议先学习完本人的博文--<详解 继承(上)-- 工具的抽象与分层> 在本人之前的博文中曾讲过"基类"的知识,那么,本篇博文中的主题--Object类 ...