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被删除了,也只有深度为 ...
随机推荐
- Go-ethereum源码解析-Part I
1. 整体概览 makefile CANDY: .PHONY: geth android ios geth-cross swarm evm all test clean 已知phony 目标并非是由其 ...
- git冲突解决的几种办法
文章目录 git stash 栈 放弃本地修改 撤销分支 强行冲掉之前的分支 删除分支 git stash 栈 git stash git pull git stash pop 当pull出现冲突时 ...
- 使用Python统计函数绘制复杂图形matplotlib
一.堆积图 1.堆积柱状图 如果将函数bar()中的参数bottom的取值设定为列表y.列表y1代表另一个数,函数bar(x,y1,bottom=y,color="r")就会输出堆 ...
- 谷歌SEO初学者常见问题解答
最近事特多,群里很多同学都在问一些非常基础的问题,实在没时间更没心情回答. (因为有些问题很基础,这些基础性问题根本不是一两句话能说清的,问这些问题的明显需要自己去好好学习,就跟小学生学加减法一样,自 ...
- elasticsearch5.0以上版本及head插件的安装
本文转载至:https://www.cnblogs.com/hts-technology/p/8477258.html(针对5.0以上版本) 对于es5.0以下的版本可以参考:https://www. ...
- easyui datagrid 后台分页,前端如何处理
module.exports = { queryMethod(){ let params = checkQueryParams.call(this); if (!params) { return; } ...
- CentOS6.5 - yum对Mysql的安装与配置
一.mysql的安装 1.查看是否安装mysql [root@localhost ~]# rpm -qa | grep mysql 如果有进行卸载(以下三种方式选一种即可): -.el6.x86_64 ...
- 根据图片URL获取图片的尺寸【Swift语言实现】
import UIKit extension UIImage { /// 获取网络图片尺寸 /// /// - Parameter url: 网络图片链接 /// - Returns: 图片尺寸siz ...
- OpenGL 3D旋转的木箱
学习自: https://learnopengl-cn.github.io/01%20Getting%20started/08%20Coordinate%20Systems/#3d 0,首先添加glm ...
- 【转】Android-Input Getevent
https://source.android.com/devices/input/getevent Getevent getevent 工具可在设备上运行,并可提供关于输入设备和内核输入事件的实时转储 ...