CF530D sum in the tree
很简单的一道贪心题
首先,先想想怎么判断是否合法
题目中说,a是自然数,那么子节点的s明显是不能比父节点大的,如果比父节点大,不合法!
所有深度为偶数的点的s被删除了,也只有深度为偶数的点被删除了,所以如果深度为奇数的点被删除了,或者有深度为偶数的点没有被删除,不合法!
所有不合法的情况我们已经判断完了,下面考虑如何使权值和最小
对于奇数层的点我们肯定是无法影响了,只有通过控制偶数层的点的权值才能影响总的权值和,而且只能影响自己和自己子节点的权值
我们从简单的入手,对于偶数层叶子结点,我们直接让它的\(s\)等于父节点的\(s\)即可,这样\(a\)为\(0\),合法且最优
对于有一个子节点的偶数层节点,取值在\([s_{fa},s_{son}]\)中任意取值,总的权值和都是不变的。
简单证明一下,就是\(a_{son}=s_{son}-s_v,a_{v}=s_v-s_{fa},a_v+a_{son}=s_{son}-s_{fa}\),而两个\(s\)都是已知的,所以这里的s爱取啥取啥,只要合法就行
那么对于子节点大于1的偶数层节点我们怎么处理呢,我们设子节点的权值不全相同
那么\(a_v=s_v-s_{fa},\sum a_{son}=\sum (s_{son}-s_v)\)
总的和就是\(\sum(s_{son}-s_v)+s_v-s_{fa}\)
显然,当\(s_v\)越小时,这个式子的值也就越小,同时我们还要保证合法,所以\(s_v\)我们就取最小的\(s_{son}\)
为了程序的简练,我们将三种情况合并,简述为
若存在子节点则为最小的\(s_{son}\),否则为\(s_{fa}\)
由于数据范围不小,记得开long long
就是这么简单,下面上代码,由于讲的已经很清楚了,代码就不加注释了
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cctype>
#ifdef ONLINE_JUDGE
#define puts(o) puts("I AK IOI\n")
#endif
#define ll long long
#define gc getchar
#define maxn 100005
using namespace std;
inline ll read(){
ll a=0;int f=0;char p=gc();
while(!isdigit(p)){f|=p=='-';p=gc();}
while(isdigit(p)){a=(a<<3)+(a<<1)+(p^48);p=gc();}
return f?-a:a;
}int n,ans;ll sum;
struct ahaha{
int to,next;
}e[maxn];int tot,head[maxn];
inline void add(int u,int v){
e[tot]={v,head[u]};head[u]=tot++;
}
int f[maxn],dep[maxn];ll s[maxn],s1[maxn];
void dfs(int u,int fa){
for(int i=head[u];~i;i=e[i].next){
int v=e[i].to;
if(~s[v]){
if((dep[v]&1)^1){ans=1;return;}
}
else{
if(dep[v]&1){ans=1;return;}
s[v]=(s1[v]==1000000001?s[u]:s1[v]);
}
if(s[v]<s[u]){ans=1;return;}
sum+=s[v]-s[u];
dfs(v,u);if(ans)return;
}
}
ll dfs(int u){
for(int i=head[u];~i;i=e[i].next){
int v=e[i].to;
dep[v]=dep[u]+1;
s1[u]=min(s1[u],dfs(v));
}
return s[u];
}
int main(){memset(head,-1,sizeof head);
n=read();dep[1]=1;
for(int i=2;i<=n;++i)
add(read(),i);
for(int i=1;i<=n;++i){
s[i]=read();
s1[i]=1000000001;
}
sum+=s[1];dfs(1);dfs(1,-1);
if(ans)
puts("-1");
else
printf("%I64d\n",sum);
return 0;
}
CF530D 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号结 ...
- CF-1099 D. Sum in the tree
CF-1099 D. Sum in the tree 题意:结点序号为 1~n 的一个有根树,根序号为1,每个点有一个权值a[i], 然后定义一s[i]表示从根节点到 结点序号为i的结点的路途上所经过 ...
- 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 ...
- D. Sum in the tree(树形+贪心)
题目链接;http://codeforces.com/contest/1099/problem/D 题目大意:给出一棵树,每个节点到根节点的路径上经过的所有点的权值之和,其深度为偶数的节点的信息全部擦 ...
- Codeforces1099D.Sum in the tree(贪心)
题目链接:传送门 思路: 一个节点放的数越大,那么以它为根的子树的节点权值之和就越小. 所以我们要在合法的范围内,使偶数层节点的权值尽可能地大.也就是说,令它的权值是子节点的最小值,这样保证了它的子节 ...
- 【LeetCode OJ】Binary Tree Maximum Path Sum
Problem Link: http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ For any path P in a bina ...
- [LeetCode] Path Sum II 二叉树路径之和之二
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
随机推荐
- SQLite3日期与时间,常见函数
SQLite3日期与时间,常见函数 import sqlite3 #con = sqlite3.connect('example.db') con = sqlite3.connect(":m ...
- 【LG4585】[FJOI2015]火星商店问题
[LG4585][FJOI2015]火星商店问题 题面 bzoj权限题 洛谷 \(Notice:\) 关于题面的几个比较坑的地方: "一天"不是一个操作,而是有0操作就相当于一天开 ...
- CF 1083 B. The Fair Nut and Strings
B. The Fair Nut and Strings 题目链接 题意: 在给定的字符串a和字符串b中找到最多k个字符串,使得不同的前缀字符串的数量最多. 分析: 建出trie树,给定的两个字符串就 ...
- angularJS ng-repeat中的directive 动态加载template
有个需求,想实现一个html组件,传入不同的typeId,渲染出不同的表单元素. <div ng-repeat="field in vm.data"> <magi ...
- git在windows7下面使用
1. 首选安装. 2. 打开Git Bash 3. 输入,就是配置一下用户名啥的 $ git config --global user.name "Jack Liao" $ git ...
- [vijos1067]Warcraft III 守望者的烦恼
就是上次考得fyfy.竟然是原题... // It is made by XZZ #include<cstdio> #include<algorithm> #include&l ...
- new与alloc/init的区别
alloc:分配内存. init:初始化. new:代替上面两个函数:分配内存,并且初始化. 注意: 1.在实际开发中很少会用到new,一般创建对象时我们一般是 [[className alloc]i ...
- 《深入分析Java Web技术内幕》读书笔记之JVM内存管理
今天看JVM的过程中收获颇丰,但一想到这些学习心得将来可能被遗忘,便一阵恐慌,自觉得以后要开始坚持做读书笔记了. 操作系统层面的内存管理 物理内存是一切内存管理的基础,Java中使用的内存和应用程序的 ...
- PHPCMS的产品筛选功能
如下图所示功能: 首先,用下面这些代码替换掉phpcms/libs/functions/extention.func.php的内容 <?php /** * extention.func.php ...
- 译 - Cassandra 数据建模的基本规则
Basic Rules of Cassandra Data Modeling 原文地址:http://www.datastax.com/dev/blog/basic-rules-of-cassandr ...