Codeforces Round #530 (Div. 2) D. Sum in the tree 树上贪心
D. Sum in the tree
题意
给出一颗树,奇数层数的点有值,值代表从1到该点的简单路的权值的和,偶数层数的点权值被擦去了 问所有节点的和的最小可能是多少
思路
对于每一个-1(也就是值未知的点)其所填的值的最小值小于等于他的自己点中已知的点的权值 换个说法就是 一个-1点有多个子树 那么这个点的值最小也是这几个子树里面的最大的值,才能使得其合法 从根节点递归求值即可
其中注意 这里计算权值和的时候,因为已知每个点到根节点的路径的点的权值和所以计算一个节点以及其子树到根节点的权值和的,等于这个节点的每个子节点的树到根节点的权值和的和减去这一个点可以取得的最小值*(支路数-1) 也就相当于一个容斥关系
ps:当时我是怎么写出那么毒瘤的代码的。。。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+5;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define pii pair<int ,int >
#define mkp make_pair
#define int long long
const int inf=0x3f3f3f3f;
int head[maxn];
int s[maxn];
int size=0;
struct Node{
int to,next;
}edge[maxn];
void add(int x,int y){
edge[size].to=y;
edge[size].next=head[x];
head[x]=size++;
}
int ans=0;
int ok=1;
pair<int,int> dfs(int x,int fa,int nowmax){
int sum=0;
int cnt=0;
int minnum=0x3f3f3f3f;
for(int i=head[x];i!=-1;i=edge[i].next){
int y=edge[i].to;
if(fa!=y){
cnt++;
if(nowmax>s[y]&&s[y]!=-1){
printf("-1\n");
ok=0;
return mkp(-1,-1);
}
pii tmp=dfs(y,x,max(nowmax,s[y]));
sum+=tmp.F;
minnum=min(minnum,tmp.S);
}
}
// cout<<x<<" "<<sum<<" "<<nowmax<<" "<<minnum<<endl;
if(cnt==0)return mkp(nowmax,nowmax);
return mkp(sum-(cnt-1)*minnum,nowmax);
}
int32_t main(){
int n;
scanf("%lld",&n);
int tmp;
memset(head,-1,sizeof(head));
size=0;
for(int i=2;i<=n;i++){
scanf("%lld",&tmp);
add(tmp,i);
}
for(int i=1;i<=n;i++){
scanf("%lld",&s[i]);
}
int num=dfs(1,-233,s[1]).F;
if(ok)cout<<num<<endl;
return 0;
}
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 题目链接: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 Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想
题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...
- Codeforces Round #530 (Div. 1)
A - Sum in the tree 就是贪心选尽量让上面的点权尽量大,那么对于偶数层的点,其到根节点的和即为所有儿子中的最大值. #include<bits/stdc++.h> usi ...
- 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. 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块饼干,在这个节点上的每块饼干 ...
随机推荐
- IO流学习之字节流(一)
IO流(Input/Output) 简介: 流是一种抽象概念,它代表了数据的无结构化传递.按照流的方式进行输入输出,数据被当成无结构的字节序或字符序列.从流中取得数据的操作称为提取操作,而向流中添加数 ...
- CF1227F2 Wrong Answer on test 233 (Hard Version)
题意 \(n\)道题,每道题有\(k\)种选项,其中第\(i\)道题正确答案是\(a_i\),但是填答案的时候填错啦,第一道题的选择填到了第二道题...第\(n\)道题的选择填到了第一道题,求在\(k ...
- Truck History POJ - 1789 板子题
#include<iostream> #include<cstring> #include<algorithm> #include<stdio.h> u ...
- OI学习之路上的宝藏网站/App分享
OI学习之路上的宝藏网站/App分享 想要变强吗少年?这里有各种我平时收集的网站/App,它们可以帮助你更好地学习算法或者找到解题思路.废话不多说,快来打开新世界的大门罢~ 知识学习 觉得各种知识晦涩 ...
- Dev 控件笔记1 repositoryItemLookUpEdit 控件
repositoryItemLookUpEdit 嵌套在 gridcontrol 中 作为列下拉 效果就是多列的 combox 代码如下 var y = userinfo.Select ...
- redis中获取没有设置ttl过期时间的key
需求:redis作为一个内存型的数据库,我们需要对过期key保持关注,从info keyspace中可以看出有多少key没有设置过期时间,那么到底是哪些呢? 说明:关于redis ttl 的返回值,请 ...
- harbor仓库部署时启用https时的常见错误KeyError: 'certificate'等
出现 KeyError: 'certificate' 错误 先确认你的配置是否正确,例如harbor.yml里的https证书位置是否正确,证书是否正常无误 如果上述无误确反复报错,请确认你的harb ...
- 【Unity|C#】基础篇(2)——栈与堆、值类型与引用类型
传送门:https://www.cnblogs.com/moonache/p/6008048.html [笔记] 图1:值类型与引用类型 存储方式 > 值类型:数据直接存在栈中 > 引用类 ...
- 将HTML保存为PDF
使用的是 jsPDF 引用 <script src="https://code.jquery.com/jquery-git.js"></script> ...
- gulp 对js\css进行md5 加密
有的服务器为了网站加载速度,会做服务器缓存,这样就会出现代码出现引用地址没变,新代码不生效,而我们又希望新代码可以及时生效,这样我们就需要避免缓存,当然也有很多方法,本文只介绍使用gulp-rev(对 ...