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 vertex vv initially had an integer number av≥0av≥0 written on it. For every vertex vv Mitya has computed svsv: the sum of all values written on the vertices on the path from vertex vv to the root, as well as hvhv — the depth of vertex vv, which denotes the number of vertices on the path from vertex vv to the root. Clearly, s1=a1s1=a1 and h1=1h1=1.
Then Mitya erased all numbers avav, and by accident he also erased all values svsv for vertices with even depth (vertices with even hvhv). Your task is to restore the values avav for every vertex, or determine that Mitya made a mistake. In case there are multiple ways to restore the values, you're required to find one which minimizes the total sum of values avav for all vertices in the tree.
Input
The first line contains one integer nn — the number of vertices in the tree (2≤n≤1052≤n≤105). The following line contains integers p2p2, p3p3, ... pnpn, where pipi stands for the parent of vertex with index ii in the tree (1≤pi<i1≤pi<i). The last line contains integer values s1s1, s2s2, ..., snsn (−1≤sv≤109−1≤sv≤109), where erased values are replaced by −1−1.
Output
Output one integer — the minimum total sum of all values avav in the original tree, or −1−1 if such tree does not exist.
Examples
input
Copy
5
1 1 1 1
1 -1 -1 -1 -1
output
Copy
1
input
Copy
5
1 2 3 1
1 -1 2 -1 -1
output
Copy
2
input
Copy
3
1 2
2 -1 1
output
Copy
-1
题意:给出一棵树,以及每个节点到根节点的路径上经过的所有点的权值之和(包括这个节点在内),其中题目把所有深度为偶数的节点的信息全部擦除了,也就是用-1表示,让你求最终所有点权之和(要求最小)。
思路:奇数层的信息都是已知的,我们只需要求出偶数层的就行了,只需要使得偶数层节点的s[i]等于其所有子节点s[i]的最小值就行了
#include <cstdio>
#include <map>
#include <iostream>
#include<cstring>
#include<bits/stdc++.h>
#define ll long long int
#define M 6
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
struct node{
int next;
int to;
};
node edge[];
int head[];
ll v[];
int cnt;
ll ans;
bool ff;
void add(int u,int v){
edge[++cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt;
}
void init(){
cnt=;
ff=;
ans=;
memset(head,,sizeof(head));
}
void dfs(int u,int f){
if(v[u]==-){ //如果是偶数层就统计最小子节点
ll minn=inf;
for(int i=head[u];i!=;i=edge[i].next){
int to=edge[i].to;
if(to==f) continue;
minn=min(minn,v[to]);
}
// cout<<minn<<endl;
v[u]=minn;
}
for(int i=head[u];i!=;i=edge[i].next){
int to=edge[i].to;
if(to==f) continue;
dfs(to,u);
if(v[to]==inf) //如果叶子节点是偶层就等于上面一层
v[to]=v[u];
}
}
void dfs_ans(int u,int f){
if(!ff) return ;
for(int i=head[u];i!=;i=edge[i].next){
int to=edge[i].to;
if(to==f) continue;
ll temp=v[to]-v[u];
if(temp<){ //如果有前缀和有矛盾就出现了错误
ff=;
return ;
}
ans+=temp;
dfs_ans(to,u);
}
}
int main(){
ios::sync_with_stdio(false);
int n;
while(cin>>n){
init();
for(int i=;i<=n;i++){
int t; cin>>t;
add(t,i);
add(i,t);
}
for(int i=;i<=n;i++)
cin>>v[i];
dfs(,);
ans+=v[]; //先加上根结点
dfs_ans(,);
if(!ff) cout<<"-1"<<endl;
else cout<<ans<<endl;
}
return ;
}
codeforces #530 D(Sum in the tree) (树上贪心)的更多相关文章
- Codeforces Round #530 (Div. 2) D. Sum in the tree 树上贪心
D. Sum in the tree 题意 给出一颗树,奇数层数的点有值,值代表从1到该点的简单路的权值的和,偶数层数的点权值被擦去了 问所有节点的和的最小可能是多少 思路 对于每一个-1(也就是值未 ...
- Codeforces1099D.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号结 ...
- 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 1099 D. Sum in the tree-构造最小点权和有根树 贪心+DFS(Codeforces Round #530 (Div. 2))
D. Sum in the tree time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- BZOJ 3221: [Codechef FEB13] Obserbing the tree树上询问( 可持久化线段树 + 树链剖分 )
树链剖分+可持久化线段树....这个一眼可以看出来, 因为可持久化所以写了标记永久化(否则就是区间修改的线段树的持久化..不会), 结果就写挂了, T得飞起...和管理员拿数据调后才发现= = 做法: ...
- Codeforces 914H Ember and Storm's Tree Game 【DP】*
Codeforces 914H Ember and Storm's Tree Game 题目链接 ORZ佬 果然出了一套自闭题 这题让你算出第一个人有必胜策略的方案数 然后我们就发现必胜的条件就是树上 ...
- [BZOJ 3221][Codechef FEB13] Obserbing the tree树上询问
[BZOJ 3221]Obserbing the tree树上询问 题目 小N最近在做关于树的题.今天她想了这样一道题,给定一棵N个节点的树,节点按1~N编号,一开始每个节点上的权值都是0,接下来有M ...
随机推荐
- CMake--常用指令
1 . ADD_DEFINITIONS 向 C/C++ 编译器添加 -D 定义,比如 在CMakeList.txt文件中添加: ADD_DEFINITIONS(-DENABLE_DEBUG -DABC ...
- 国内的go get问题的解决
在国内采用go get有时会下载不到一些网站如golang.org的依赖包. 方法1(亲测有效): gopm 代替go 下载第三方依赖包可以采用gopm从golang.org一些镜像网站上下载. 注意 ...
- 关于IWMS后台登录问题总结
一.登录后台,点击登录无反应: 1.是因为网站文件夹没有权限,需要右击文件夹,将只读勾选去掉 2.在安全中加入Everyone对象. 二.登录后台后,左边显示不全,是因为会员权限不够,需要给权限.
- PLSQL 错误问题:Datebase character set (AL32UTF-8) and Client character set (ZHS16GBK) are different.
(解决不了,网上用的是Orecal,我用的只是客户端.) 网上找到解决方法 打开注册表(ctr+R,输入regedit),根据报错提示找到注册表位置,但本机是win10 64位系统,根据以上路径找不到 ...
- vue之v-for使用说明
demo.html <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/19 ...
- ECS配置lamp环境
1.安装apache 1.1 安装apache [root@nmserver-7 ~]# yum install httpd httpd-devel 1.2 启动apache服务 [root@nmse ...
- Play framework框架中通过post方式发送请求
搞了好久这个最终还是在play官方文档中看见的发送请求的方式,国内好像很少有使用这个框架的,加之自己不是太愿意宣传,好东西总归是好东西,不说废话了. 在play中发送请求有两种常用的方式,一种get, ...
- linux 目录分类与文件操作
/ 虚拟根目录 一般不会在这里存储文件 /bin 二进制目录,存放需要GNU用户级的工具 /boot 启动目录,存放启动文件 /dev 设备目录,linux在这里创建设备节点 /etc 系统配置文件目 ...
- 【数学建模】day07-数理统计II
方差分析和回归分析. 用数理统计分析试验结果.鉴别各因素对结果影响程度的方法称为方差分析(Analysis Of Variance),记作 ANOVA. 比如:从用不同工艺制作成的灯泡中,各自抽取了若 ...
- sql练习题及经典题
https://blog.csdn.net/mrbcy/article/details/68965271 经典例题 19.查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录. S ...