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 ...
随机推荐
- BZOJ2039_employ人员雇佣_KEY
题目传送门 网络流,求最小割. 设tot为所有盈利的和,即所有人(不花钱)雇佣. 对于S->i建一条容量为c[i]的边,i->j建一条S[i][j]*2的边,之所以这样建是因为如果不选这个 ...
- 使用三层交换机实现不同vlan的互通
如下拓扑图所示,要实现vlan10(192.168.10.0/24)与vlan 20(192.168.20.0/24)的网络互通. 三层交换机配置: 创建vlan:Switch#configure t ...
- js页面动态时间展示
效果图: 具体代码 js代码 <script type="text/javascript"> var t = null; t = setTimeout(time,100 ...
- Spring学习(二)-----eclipse新建spring项目
一:准本工作(下载需要的jar包) 1.下载准备Spring-framework-4.2.0 链接为: http://repo.springsource.org/libs-release-local/ ...
- HttpClient使用详解 (一)
Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客户端发送Http请求变得容易,而且 ...
- [ASP.NET Core] 建置x86版本 (workaround)
前言 本篇文章介绍如何建置ASP.NET Core项目的x86版本输出(workaround),为自己留个纪录也希望能帮助到有需要的开发人员. ASP.NET Core官网 步骤 首先到微软官网的「. ...
- PLSQL变量和类型,流程控制语句,集合
---PLSQL 调试授权 GRANT debug any procedure, debug connect session TO scott; --定义变量 declare part_number ...
- I understand that you would like to know about the Amazon Giveaway
Dear Seller, Greetings from Amazon Seller Support. From your mail, I understand that you would like ...
- Python 深浅复制
(一)浅复制 复制列表最简单的方式是使用内置类型的构造方法: >>> l1 = [1, [2, 3], (4, 5)] >>> l2 = list(l1) > ...
- FPGA选型
工欲善其事必先利其器,开发FPGA的第一步,当然是选择一片符合你设计需求的芯片. 但是芯片种类那么多,老板又要你越省越好,硬件工程师也天天问你到底该用哪块芯片,怎么办? 今天正好可以跟大家聊聊这些问题 ...