Codeforces Round #530 (Div. 2)
RANK :2252 题数 :3
补题:
思路:贪心 把权值放在祖先节点上 ,预处理 每个节点保存 他与他儿子中 权值最小值即可。
最后会有一些叶子节点依旧为 INF 权值按0算即可,然后其他的权值计算为 它 - 它父亲的。
注意判断时候会出现父亲比儿子大的这种非法情况。
#include<bits/stdc++.h>
using namespace std;
#define inf 0x7f7f7f7f
#define ll long long
#define maxn 123456
ll fa[maxn],n,s[maxn],ans;
int main()
{
scanf("%lld",&n);
for(int i=2; i<=n; i++)
scanf("%lld",&fa[i]);
for(int i=1; i<=n; i++)
{
scanf("%lld",&s[i]);
if(s[i]==-1)s[i]=inf;
}
ans=s[1];
for(int i=2; i<=n; i++)
s[fa[i]]=min(s[fa[i]],s[i]);
for(int i=2; i<=n; i++)
{
if(s[i]==inf)s[i]=0;
else
{
ans+=s[i]-s[fa[i]];
if(s[i]-s[fa[i]]<0)
{
printf("-1\n");
return 0;
}
}
}
printf("%lld\n",ans);
return 0;
}
Codeforces Round #530 (Div. 2)的更多相关文章
- Codeforces Round #530 (Div. 2) F (树形dp+线段树)
F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...
- 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):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) F 线段树 + 树形dp(自下往上)
https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...
- Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)
题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...
- Codeforces Round #530 (Div. 2) C D
C: *可以保留删除或者增加 ? 保留或者删除 #include<bits/stdc++.h> using namespace std; int main(){ string s; int ...
- Codeforces Round #530 Div. 1 自闭记
A:显然应该让未确定的大小尽量大.不知道写了啥就wa了一发. #include<iostream> #include<cstdio> #include<cmath> ...
- Codeforces Round #530 (Div. 2) F - Cookies
F - Cookies 思路:我们先考虑如何算出在每个节点结束最多能吃多少饼干, 这个dfs的时候用线段树维护一下就好了, 然后有个这个信息之后树上小dp一下就好啦. #include<bits ...
- Codeforces Round #530 (Div. 1)
A - Sum in the tree 就是贪心选尽量让上面的点权尽量大,那么对于偶数层的点,其到根节点的和即为所有儿子中的最大值. #include<bits/stdc++.h> usi ...
随机推荐
- 后RCNN时代的物体检测及实例分割进展
https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&mid=2650736740&idx=3&sn=cdce446703e69b ...
- npx简介(转载)
npm v5.2.0引入的一条命令(npx),引入这个命令的目的是为了提升开发者使用包内提供的命令行工具的体验. 举例:使用create-react-app创建一个react项目. 老方法: npm ...
- Android 基础 一 AndroidManifest.xml
一.概述 AndroidManifest.xml是Android应用的入口文件,它描述了package中暴露的组件(activities, services, 等等),他们各自的实现类,各种能被处理的 ...
- Python中的日志处理
在日常项目中,总是需要记录下一些细小信息或者错误码.错误信息的,这个时候就需要进行日志的操作.python中用于日志创建.设置和记录等功能的模块,就是logging了,下面是对其基本使用方法的介绍: ...
- caffe关闭建立网络的log输出
C++ google::InitGoogleLogging("XXX"); google::SetCommandLineOption("GLOG_minloglevel& ...
- 【专栏学习】APM——异步编程模型(.NET不推荐)
(1)learning hard C#学习笔记 异步1:<learning hard C#学习笔记>读书笔记(20)异步编程 (2)<C# 4.0 图解教程> 22.4 异步编 ...
- hbase启动后HMaster进程自动关闭
1.情况描述如题所示,hbase启动以后,HMaster进程启动了,几秒钟以后自动关闭,但是HRegionServer进程正常运行: 原因是,hdfs的默认端口号是8020,而我core-site.x ...
- 一脸懵逼搭建Zookeeper分布式集群
1:首先将http://zookeeper.apache.org/ 下载好的zookeeper-3.4.5.tar.gz上传到三台虚拟机上,之前博客搭建好的(安装Zookeeper之前记得安装好你的j ...
- Redis监控和告警
https://blog.csdn.net/isoleo/article/details/52981140
- nginx proxy_pass指令’/’注意事项
1. proxy_pass配置说明 不带/ location /test/ { proxy_pass http://t6:8300; } 带/ location /test/ { proxy_pass ...