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 ...
随机推荐
- [Swift]DJSet
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- IT兄弟连 JavaWeb教程 Servlet会话跟踪 Cookie路径问题
操作Cookie时,需要注意路径问题: 设置操作:任何路径都可以设置Cookie,但是有时我们也是用设置进行替换Cookie和删除Cookie(maxAge=0)! 替换:只能由完全相同的路径来操作! ...
- seq(2018.10.24)
一道\(dp\)题... 期望\(40\)分解法 预处理:离散化,然后让连续一段值相同的元素合并为一个元素. 正式\(DP\): 显然有个最差策略为每个元素处都切一次,则切的次数为元素的个数\(-1\ ...
- AtCoder Beginner Contest 051 ABCD题
A - Haiku Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement As a New Yea ...
- 115 Distinct Subsequences 不同子序列
给定一个字符串 S 和一个字符串 T,求 S 的不同的子序列中 T 出现的个数.一个字符串的一个子序列是指:通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串.(譬如," ...
- 遍历list集合的三种方式
List<String> list1 = new ArrayList<String>(); list1.add("1"); list1.add(" ...
- asp也玩三层架构(有源代码)
实体类 <% Class UserInfo Private mintId Public Property Let UserId(intUserId) mintId = intUserId End ...
- python搭建ftp服务器
1 # coding: utf-8 import os from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.handler ...
- hihocoder1032 最长回文子串
思路: manacher模板. 实现: #include <iostream> #include <cstring> using namespace std; ]; strin ...
- ListView与ScrollView冲突的4种解决方案
问题解决方案1.手动设置ListView高度 经过测试发现,在xml中直接指定ListView的高度,是可以解决这个问题的,但是ListView中的数据是可变的,实际高度还需要实际测量.于是手动 ...