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的更多相关文章

  1. 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号结 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...

  6. pat 甲级 1099. Build A Binary Search Tree (30)

    1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  7. Codeforces Round #530 (Div. 2) D. Sum in the tree 树上贪心

    D. Sum in the tree 题意 给出一颗树,奇数层数的点有值,值代表从1到该点的简单路的权值的和,偶数层数的点权值被擦去了 问所有节点的和的最小可能是多少 思路 对于每一个-1(也就是值未 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. XML标准和RFC官方文档

  2. B - Dungeon Master POJ - 2251

    //纯bfs #include <iostream> #include <algorithm> #include <cstring> #include <cs ...

  3. Log4j2 - Unable to invoke factory method in class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFileAppender for element RollingFile

    问题与分析 在使用Log4j2时,虽然可以正确读取配置文件并生成log文件,但偶然发现控制台打印了异常信息如下: 2018-12-31 17:28:14,282 Log4j2-TF-19-Config ...

  4. Gitlab备份,Crontab定时备份

    1:Gitlab备份非常简单,只需要一条命令就可以创建完整的备份 gitlab-rake gitlab:backup:create 使用以上命令,就相当于在/var/opt/gitlab/backup ...

  5. 推荐 VS2010入门教程—鸡啄米

    http://www.jizhuomi.com/catalog.asp?tags=VS2010 推荐 VS2010入门教程—鸡啄米,真的非常使用和经典!

  6. Flask (七) 部署

    阿里云部署Flask项目   部署Flask项目和部署Django项目基本一致,我们也使用uwsgi+nginx   我们在部署Django项目基础上部署Flask项目   1, 将uwsgi.ini ...

  7. 【Codeforces1111D_CF1111D】Destroy the Colony(退背包_组合数学)

    题目: Codeforces1111D 翻译: [已提交至洛谷CF1111D] 有一个恶棍的聚居地由几个排成一排的洞穴组成,每一个洞穴恰好住着一个恶棍. 每种聚居地的分配方案可以记作一个长为偶数的字符 ...

  8. Spring+Quartz配置定时任务

    一.Quartz介绍 在企业应用中,我们经常会碰到时间任务调度的需求,比如每天凌晨生成前天报表,每小时生成一次汇总数据等等.Quartz是出了名的任务调度框架,它可以与J2SE和J2EE应用程序相结合 ...

  9. 响应式Spring Cloud初探

    响应式Spring Cloud初探 分类:工程原文链接:The Road to Reactive Spring Cloud作者:  JOSH LONG译者: helloworldtang日期:JUNE ...

  10. Ionic之button标签ng-click无反应解决

    在使用Ionic中,使用按钮的ng-click事件,竟然点击没有反应,刚开始以为自己写得方法有问题才会不起作用,自己在点击之后就console.log()一个东西,但是console也是无法反应的.& ...