CF-1099 D. Sum in the tree
CF-1099 D. Sum in the tree
题意:结点序号为 1~n 的一个有根树,根序号为1,每个点有一个权值a[i]
, 然后定义一s[i]
表示从根节点到 结点序号为i
的结点的路途上所经过的结点的权值之和。
如图所示有:
s[1] = 1
s[2] = 2
s[3] = 2
s[4] = 2
s[5] = 2
而现在的情况是:所有的a我们都不知道,只知道部分的s,然后需要我们求出对a求和的最小值。
考虑一般情况,每一个结点有很多子结点。为了描述清楚,对于某个结点i,a为 i 的权值a[i],s为i的s[i]。
要使得a的和最小,那么对于每一个结点,就要求出最小权值。如果一个结点 i 有很多子结点,假设这些子结点中最小的s 为 min,那么我们可以让 a[i]=min
,使得结点 i 的权值对所有子结点的s有贡献,只有这样才能保证得到的答案是最小的。
经过上述的处理之后,对于现在还不清楚的点(s不知道的点),可以想到它们的权值为0也是同样可以满足的。所以直接将他们的权值赋值为0即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,s[200005],p[200005],ans=0;
int main(){
cin>>n;
for(int i=2;i<=n;i++)
cin>>p[i];//p[i]为i的父节点
for(int i=1;i<=n;i++){
cin>>s[i];
if(s[i]==-1)s[i]=1e10;//标记为1e10是为了消除-1对我们得到min(分析中的min)的影响。
}
for(int i=1;i<=n;i++){
s[p[i]]=min(s[p[i]],s[i]);//每次对i的父节点的s进行更新,
}
for(int i=1;i<=n;i++){
if(s[p[i]]>s[i]){
cout<<-1;return 0;//如果父节点的s大于当前结点的s,则输出-1
}
if(s[i]==1e10)s[i]=s[p[i]];//如果s不清楚,则a[i]=0,s[i]=s[p[i]]
ans+=s[i]-s[p[i]];//如果s清楚,则a[i]=s[i]-s[p[i]];
}
cout<<ans<<endl;
return 0;
}
CF-1099 D. 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号结 ...
- PAT 1099 Build A Binary Search Tree[BST性质]
1099 Build A Binary Search Tree(30 分) A Binary Search Tree (BST) is recursively defined as a binary ...
- 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 ...
- 1099 Build A Binary Search Tree
1099 Build A Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a bi ...
- PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...
- pat 甲级 1099. Build A Binary Search Tree (30)
1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- Codeforces Round #530 (Div. 2) D. Sum in the tree 树上贪心
D. Sum in the tree 题意 给出一颗树,奇数层数的点有值,值代表从1到该点的简单路的权值的和,偶数层数的点权值被擦去了 问所有节点的和的最小可能是多少 思路 对于每一个-1(也就是值未 ...
- 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 ...
- codeforces #530 D(Sum in the tree) (树上贪心)
Mitya has a rooted tree with nn vertices indexed from 11 to nn, where the root has index 11. Each ve ...
随机推荐
- js中 call() ,apply(),bing()方法三者的用法和区别
面试中经常会被问到的,或者做笔试题的时候也会有这样的问题,所以今天专门对这个问题做个总结: 先看个例子: var age = '19' var myObj = { name:'小赖', myAge:t ...
- ES6入门系列二(数值的扩展)
ES6 在 Number对象上新增了很多方法 1 . Number.isFinite()判断是否为有限的数字 和全局的isFinite() 方法的区别是 isFinite('1') === tr ...
- react native 安卓打包--mac环境,如果打包不成功可注意下my-release-key.keystore的位置关系(绝对路径)
// my-release-key.keystore和my-key-alias都是可修改的名称 1.生成签名密钥(keytool -genkey -v -keystore my-release-key ...
- 解读ping -n 4 127.1 >nul 2>nul
命令解读 ping是Windows.Unix和Linux系统下的一个命令.ping也属于一个通信协议,是TCP/IP协议的一部分.利用"ping"命令可以检查网络是否连通,可以很好 ...
- GYM 101673F(树计数)
树上每个割点计算一下各个size的组合相乘再相加为第一问答案,取最大的:再把本答案中最大的两个size相乘减掉,为第二问答案. const int maxn = 1e4 + 5; int n, siz ...
- 转 用好HugePage,告别Linux性能故障
超过32G 的数据库,可以是使用如下方法配置. ######### Slow Performance with High CPU Usage on 64-bit Linux with Large SG ...
- Linux--NiaoGe-Service-04
操作系统版本:CentOS 6.10 x86_64 查看内核所获取到的网卡信息 [root@xueji ~]# dmesg | grep -in eth :e1000 ::-bit) :0c::6b: ...
- android开发学习 ------- 上传本地项目到gitlab
写了一个demo,上传到gitlab 参考 https://blog.csdn.net/litianxiang_kaola/article/details/74075151 1:新建项目,填写相应的 ...
- gin 打linux环境包问题解决
打window包直接go build一下,完事, 但是,打linux包出现如下错误 ..\github.com\mattn\go-isatty\isatty_linux.go:7:8: cannot ...
- centos6.3下postgresql-9.3安装记录
Xshell for Xmanager Enterprise 4 (Build 0186) Copyright (c) 2002-2011 NetSarang Computer, Inc. All r ...