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号结点到根节点的权值和。但是有些si=-1,这就相当于丢失了当前结点的数据。
假设原本每个点的权值为ai,那么现在求sum{ai}的最小为多少,ai为非负数。
题解:
这题可以单独看每一条链上的s值,假设当前结点为u,儿子结点v,那么就有几种情况:
1.su==-1&&sv==-1,这种不用管,继续往下看;
2.su==-1&&sv>=0,这种情况,su以及上面可能有多个si为-1的结点,但这里我们可以就把它看作一个点,然后找到非-1父亲结点的s值,假设为p,那么sv-sp就是中间结点的权值和,看作一个点的话,就是那个点的a值,同时根据这个我们可以计算出其sum值。至于这里怎么求sp,可以在dfs的时候记录一下。
3.su>=0&&sv==-1,这里就需要上面的记录了,记录目前的su值,方便后面找sp;
4.su>=0&&sv>=0,这里直接往下搜索就行了。
结合上面的分析,我们只需要一个dfs记录一下就好了,最后求au的时候就是sumu-sump,也可以在dfs的过程中处理。
但是我们刚才只是对链的分析,一个结点可能有多个儿子结点。想一下,会发现只有上面第2种情况会多考虑一点,因为根据哪个儿子结点来确定当前的s是一个问题。
这个问题也不难解决,取min{sv-sp}即可,一方面是让其尽量大,另一方面是保证方案可行。
最后再判断一下可行性就行了。
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+;
int n;
vector <int> g[N];
ll sum[N],b[N];
int flag = ;
void dfs(int u,ll a,int fa){
int son = ;
if(flag) return ;
int f=;
for(auto v:g[u]){
if(v==fa) continue ;
if((sum[u]==- && sum[v]>=) ||f){
f=;
ll now=sum[v]-a;
if(sum[u]==-) sum[u]=now+a;
if(sum[u]!=-) sum[u]=min(sum[u],now+a);
}
}
for(auto v:g[u]){
if(v==fa) continue ;
son++;
if(sum[v]==- && sum[u]==-){
dfs(v,a,u);
continue ;
}
dfs(v,sum[u],u);
}
if(son==&&sum[u]==-){
b[u]=;
return ;
}
if(sum[u]!=-) b[u]=sum[u]-a;
}
int main(){
cin>>n;
for(int i=;i<=n;i++){
int f;
scanf("%d",&f);
g[f].push_back(i);
g[i].push_back(f);
}
for(int i=;i<=n;i++) scanf("%I64d",&sum[i]);
if(sum[]==-) sum[]=;
dfs(,,-);
for(int i=;i<=n;i++){
if(b[i]<) flag=;
}
if(flag) puts("-1");
else{
ll ans = ;
for(int i=;i<=n;i++) ans+=b[i];
cout<<ans;
}
return ;
}
/*
7
1
2
1
4 4 5
0 -1 3 -1 -1 3 -1
*/
Codeforces Round #530 (Div. 2):D. Sum in the tree (题解)的更多相关文章
- 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 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 #530 (Div. 2) A,B,C,D
A. Snowball 链接:http://codeforces.com/contest/1099/problem/A 思路:模拟 代码: #include<bits/stdc++.h> ...
- Codeforces Round #530 (Div. 2)
RANK :2252 题数 :3 补题: D - Sum in the tree 思路:贪心 把权值放在祖先节点上 ,预处理 每个节点保存 他与他儿子中 权值最小值即可. 最后会有一些叶子节点依旧为 ...
- Codeforces Round #530 (Div. 1)
A - Sum in the tree 就是贪心选尽量让上面的点权尽量大,那么对于偶数层的点,其到根节点的和即为所有儿子中的最大值. #include<bits/stdc++.h> usi ...
- Codeforces Round #530 (Div. 2) Solution
A. Snowball 签. #include <bits/stdc++.h> using namespace std; ], d[]; int main() { while (scanf ...
- Codeforces Round #530 (Div. 2) F (树形dp+线段树)
F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...
- Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)
https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...
随机推荐
- 【带 josn参数的测法】
遇到json 参数的情况这样写 ,否则就会报错 cod 415 nocookie post请求 ,","email":"beihe@163.com&quo ...
- Ducci序列 (Ducci Sequence,ACM/ICPC Seoul 2009,UVa1594)
题目描述: 题目思路: 直接模拟 #include<stdio.h> #include<string.h> #define maxn 105 int less(const ch ...
- JavaScript 常用控制流程代码范例
if-else 的用法 var a = 33 if (a == 1){ console.log ('a等于1') } else if (a==2) { console.log ('a等于2') } e ...
- Django创建App报错
在django下创建APP项目时遇到的坑 python manage.py startapp app01 报错内容如下: 解决:找到报错中的文件夹151行删除items(),)中的逗号即可 在命令行下 ...
- leetcode个人题解——two sum
这是leetcode第一题,通过较为简单. 第一题用来测试的,用的c,直接暴力法过, /** * Note: The returned array must be malloced, assume c ...
- Python3 Tkinter-Place
1.绝对坐标 from tkinter import * root=Tk() lb=Label(root,text='hello Place') lb.place(x=0,y=0,anchor=NW) ...
- c#调用c++dll(c++界面在c#显示)____制作dll
1.c++dll含界面,以摄像头dll为例: 1.直接在c++SDK上调试运行成功,生成dll. 2.留一个调用接口(格式如下,写在cpp文件中,即函数体): extern "C" ...
- Android开发 使用 adb logcat 显示 Android 日志
作者 : 万境绝尘 转载请著名出处 eclipse 自带的 LogCat 工具太垃圾了, 开始用 adb logcat 在终端查看日志; 1. 解析 adb logcat 的帮助信息 在命令行中输入 ...
- java — 值传递和引用传递
在 Java 应用程序中永远不会传递对象,而只传递对象引用.因此是按引用传递对象.Java 应用程序按引用传递对象这一事实并不意味着 Java 应用程序按引用传递参数.参数可以是对象引用,而 Java ...
- 使用Quartz.Net同时执行多个任务
在Quartz.Net中可能我们需要在某一时刻执行多个任务操作,而又不想创建多个任务.Quartz.Net为我们提供了多个ScheduleJob的重载来实现多个一次执行多个任务. // 创建一个组任务 ...