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 ...
随机推荐
- 【转】CentOS Linux解决Device eth0 does not seem to be present(linux)
原文来自:http://www.linuxidc.com/Linux/2012-12/76248.htm 在VMware里克隆出来的CentOS Linux.. ifconfig...没有看到eth0 ...
- PHP:Iterator(迭代器)接口和生成器
迭代器 可在内部迭代自己的外部迭代器或类的接口.详情:http://php.net/manual/zh/class.iterator.php 接口摘要 Iterator extends Travers ...
- Django-建立网页
进入cmd模式做 django-admin startproject helloworld创建一个project,并命名helloworld,新生成的文件结构如下 输入python manage. ...
- Windows下Mongodb安装部署
1.下载安装包 mongodb-win32-x86_64-enterprise-windows-64-3.6.4.zip 解压 安装失败(当前环境windows server2012 R2):已验证可 ...
- 百度云 win10 125%界面模糊 解决
右击图标 ->兼容性->更改高DPI设置 -> 替代高DPI缩放行为.打√
- 英特尔近日发布最新版实感™ SDK R5 (v7)
英特尔开发人员专区 原文地址 英特尔® 实感™ SDK 的 7.0.23.8048 版本(也称为 R5)现已推出.您将看到的主要变化包括: 支持英特尔® 实感™ SR300 摄像头:应于 2016 年 ...
- leetcode个人题解——#31 Next Permutation
写这题时脑子比较混乱,重写了一遍wiki大佬的解法. 算法: According to Wikipedia, a man named Narayana Pandita presented the fo ...
- LCA(Tarjan算法)模板
一.查询一组的LCA Nearest Common Ancestors A rooted tree is a well-known data structure in computer science ...
- c# apache服务器请求得到数据(初级)
1.代码: string data = new WebClient().DownloadString("http://localhost:81/123.txt");
- “我爱淘”第二冲刺阶段Scrum站立会议7
完成任务: 完成学院分类的点击查看书籍功能,可以点击书的条目查看书的详细信息.将登陆界面以及注册发布界面完善了一下修复一些bug. 计划任务: 将书的详细信息进行完善,并且可以点击收藏以及已预订等功能 ...