Codeforces1099D.Sum in the tree(贪心)
题目链接:传送门
思路:
一个节点放的数越大,那么以它为根的子树的节点权值之和就越小。
所以我们要在合法的范围内,使偶数层节点的权值尽可能地大。也就是说,令它的权值是子节点的最小值,这样保证了它的子节点权值为正。
因为奇数层的节点的s已知,所以修改偶数层的节点仅影响,向下一层的节点。(因为再往下的话,路径上的权值和不随这个偶数层的节点的权值改变而改变,而是被奇数层截断了)
代码:
#include <bits/stdc++.h> using namespace std;
const int MAX_N = 1e5 + ;
const int INF = 0x3f3f3f3f; int p[MAX_N], s[MAX_N];
int a[MAX_N]; int main()
{
int N;
cin >> N;
for (int i = ; i <= N; i++)
scanf("%d", &p[i]);
for (int i = ; i <= N; i++)
scanf("%d", &s[i]); for (int i = ; i <= N; i++) {
if (s[i] == -)
s[i] = INF;
else {
s[p[i]] = min(s[p[i]], s[i]);
}
}
a[] = s[];
for (int i = ; i <= N; i++) {
if (s[i] == INF) {
a[i] = ;
continue;
}
a[i] = s[i] - s[p[i]];
}
long long ans = ;
for (int i = ; i <= N; i++) {
ans += a[i];
if (a[i] < ) {
ans = -;
break;
}
}
cout << ans << endl;
return ;
}
Codeforces1099D.Sum in the tree(贪心)的更多相关文章
- 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 Round #530 (Div. 2):D. Sum in the tree (题解)
D. Sum in the tree 题目链接:https://codeforces.com/contest/1099/problem/D 题意: 给出一棵树,以及每个点的si,这里的si代表从i号结 ...
- CF-1099 D. Sum in the tree
CF-1099 D. Sum in the tree 题意:结点序号为 1~n 的一个有根树,根序号为1,每个点有一个权值a[i], 然后定义一s[i]表示从根节点到 结点序号为i的结点的路途上所经过 ...
- 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 ...
- D. Sum in the tree(树形+贪心)
题目链接;http://codeforces.com/contest/1099/problem/D 题目大意:给出一棵树,每个节点到根节点的路径上经过的所有点的权值之和,其深度为偶数的节点的信息全部擦 ...
- CodeForces 489C Given Length and Sum of Digits... (贪心)
Given Length and Sum of Digits... 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/F Descr ...
- POJ 2054 Color a Tree#贪心(难,好题)
题目链接 代码借鉴此博:http://www.cnblogs.com/vongang/archive/2011/08/19/2146070.html 其中关于max{c[fa]/t[fa]}贪心原则, ...
- CF530D sum in the tree
我是题面.原题地址 很简单的一道贪心题 首先,先想想怎么判断是否合法 题目中说,a是自然数,那么子节点的s明显是不能比父节点大的,如果比父节点大,不合法! 所有深度为偶数的点的s被删除了,也只有深度为 ...
随机推荐
- test--3
<script type="text/javascript">// <![CDATA[$(function () { if (isLogined &&am ...
- Freeswitch 各版本一键安装脚本 Freeswitch 快速安装 G729编解码库
最近有时间整理,写了freeswitch的一键安装包分享一下,里面带有 mysql=0 是否选择安装mysql. 其他提示:脚本里面集成了安装g729脚本,支持录音.转码的bcg729脚本. 1.下载 ...
- flask基础--第二篇
1.Flask中的HTTPResponse,Redirect, render #导入render_template和redirect from flask import Flask,render_te ...
- FlatList 核心运用
<FlatList data={this.state.stuList} renderItem={this._renderItem} keyExtactor={this._keyExtractor ...
- UML之顺序图
一 定义 顺序图是将交互关系表示为一个二维图.纵向是时间轴(生命线),时间沿竖线向下延伸.横向轴代表了在协作中各独立对象的类元角色.类元角色用生命线表示.当对象存在时,角色用一条虚线表示,当对象的过程 ...
- What to do when you have small dataset - 拥有小型数据集时该怎么办
I'm trying to train a classifier with neural network, but I've got too small datasets. Each class ha ...
- Python3的string库模板的应用
模板 字符串模板将作为内置的拼接语法的替代用法.使用Template拼接时,要在名字前加前缀$来标识变量(例如,$var).或者,如果有必要区分变量和周围的文本,可以用大括号包围变量(例如,${var ...
- react native进一步学习(NavigatorIOS 学习)
特别申明:本人代码不作为任何商业的用途,只是个人学习的一些心得,为了使得后来的更多的程序员少走一些弯路.*(如若侵犯你的版权还望见谅)*. 开发工具:WebStorm,xcode 1. rn的创建的时 ...
- android LogConfigurator
android LogConfigurator 此为第三方的 Log日志
- CSS1 !important
CSS1 !important 提升指定样式规则的应用优先权 ie6并不支持.还是会被后面的样式覆盖