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 ...
随机推荐
- 使用jmeter来发送json/gzip格式数据 --------笔记
一.使用jmeter来发送gzip数据 有时候我们需要模拟在客户端将数据压缩后, 发送(post)到服务器端. 通常这种情况,会发生在移动终端上. 这样做的好处, 是可以节省流量. 当然, 服务器返 ...
- Python3练习题 026:求100以内的素数
p = [i for i in range(2,100)] #建立2-99的列表 for i in range(3,100): #1和2都不用判断,从3开始 for j in range(2, ...
- React Native之通知栏消息提示(android)
React Native之通知栏消息提示(android) 一,需求分析与概述 1.1,推送作为手机应用的基本功能,是手机应用的重要部分,如果自己实现一套推送系统费时费力,所以大部分的应用都会选择使用 ...
- 谈谈B-树和B+树及其应用
待更!!! B-树和B+树的应用:数据搜索和数据库索引 B+/-Tree原理及mysql的索引分析 从B树.B+树.B*树谈到R 树 B树.B-树.B+树.B*树
- 机顶盒webview开发调试
安装node的anywhere插件 启动本地服务器后 使用chrome的DevTool-----> chrome://inspect/#devices 点击inspect 第一次需要FQ ...
- select、poll、epoll之间的区别(搜狗面试)
(1)select==>时间复杂度O(n) 它仅仅知道了,有I/O事件发生了,却并不知道是哪那几个流(可能有一个,多个,甚至全部),我们只能无差别轮询所有流,找出能读出数据,或者写入数据的流,对 ...
- 莫烦theano学习自修第五天【定义神经层】
1. 代码如下: #!/usr/bin/env python #! _*_ coding:UTF-8 _*_ import numpy as np import theano.tensor as T ...
- Python学习之路—————day04
今日内容: 1. 循环语句 1.1 if判断 1.2 while循环 1.3 for循环 一.if判断 语法一: if 条件 代码块1 代码块2 代码块3 # 例: sex='female' age= ...
- Simple Use IEnumerable<T>
Private static IEnumerable<T> FunDemo(T para) { while(...) { .... yield return Obj; } } static ...
- CSS3 flexbox 布局 ---- flex 容器属性介绍
flexbox布局是CSS3中新增的属性,它可以很轻松地帮我们解决掉一些常见的布局问题,比如导航栏. 我们用普通的方法写导航栏,通常会在ul, li 结构写好后,让li 元素左浮动,然后再给ul 清浮 ...