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 ...
随机推荐
- [Xcode 实际操作]八、网络与多线程-(2)使用UIApplication对象打开网页
目录:[Swift]Xcode实际操作 本文将演示如何使用应用程序单例对象,打开指定的网页. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKi ...
- IT兄弟连 JavaWeb教程 MVC设计模式
MVC是Model-View-Controller的简称,即模型-视图-控制器.MVC是一种设计模式,它强制性地把应用程序的数据展示.数据处理和流程控制分开.MVC把应用程序分成3个核心模块:模型.视 ...
- IT兄弟连 JavaWeb教程 Servlet会话跟踪 获取Session对象
Session对象的获取有两种: ● 有参方法: HttpSession request.getSession(boolean isNew) 参数: true:获取一个Session对象,如果之前S ...
- Alcatraz -- 一个神奇的管理插件的Xcode插件
Install Paste this into your terminal: curl -fsSL https://raw.githubusercontent.com/supermarin/Alcat ...
- MySQL中group by 与 order by 一起使用排序问题
假设有一个表:reward(奖励表),表结构如下: CREATE TABLE test.reward ( id ) NOT NULL AUTO_INCREMENT, uid ) NOT NULL CO ...
- 「今日 GitHub 趋势」让全世界程序员体会中国的 12306 抢票狂潮
[2018年1月7日 GitHub 趋势] No.1:yyx990803 / build-your-own-mint 单日 714 星 使用 Plaid,Google 表格和 CircleCI 构建您 ...
- VLAN-1-VLAN配置
f0/12和f0/24不在列表中,因为它们动态地成为trunk,支持多个vlan “unsup”意为这个2950交换机不支持FDDI和TR 首先 switchport mode ...
- 用apache commons-pool2建立thrift连接池
Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.具体的介绍可以看Apache的官方网站:http://thrift.apache.org/ . ...
- NOIP前模拟赛总结
NOIP前模拟赛总结 from 2018.10.7 to ??? Date Name Score(Rank) Problems 2018.10.7 McfXH AK Contest 42(?) 期望得 ...
- CentOS7下使用Docker容器化.net Core 2.2
一.使用 yum 安装(CentOS 7下) Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker . 通过 una ...