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块饼干,在这个节点上的每块饼干 ...
随机推荐
- pygame 运行心理学问卷
import pygame import sys from pygame.locals import * # wait for keys to putdown def waitForKeys(keys ...
- 一道有趣的for循环题
一道有趣的for循环题 今天在复习js基础知识时发现了一个for循环的题,第一眼看到直接懵逼了,没想到for循环竟然还可以这样玩?涨姿势了. 题目是这样的 for(i=0, j=0; i<10, ...
- MySQL数据库优化(一)
1.1.1. 慢查询日志当查询超过一定的时间没有返回结果的时候,才会记录到慢查询日志中.默认不开启.采样的时候手工开启.可以帮助我们找出执行慢的 SQL 语句查看慢 SQL 日志是否启用(on 表示启 ...
- IDEA科学使用
今天莫名激活码又用不起了有能力的支持正版吧 ,要用的时候又去网上到处找然后发现各种用不了,去淘宝又怕被骗博主就是过来人 ,总算下定决心写一篇一劳永逸的方法.. 方法一:合理使用激活码 用过idea的都 ...
- java学习笔记之集合—ArrayList源码解析
1.ArrayList简介 ArrayList是一个数组队列,与java中的数组的容量固定不同,它可以动态的实现容量的增涨.所以ArrayList也叫动态数组.当我们知道有多少个数据元素的时候,我们用 ...
- Python之六:模块
模块包含了大量的函数方法和变量,我们可以用下面的语句调用模块: import 模块名 这样我们就可以在后面的语句中使用模块中的函数或者变量了.调用时只需用 模块名.函数名的方式调用即可 from ...
- 如何成为一名AI工程师
如何成为一名AI工程师 step 前端:js,html,找准方向开始累积知识! 计算机/数学专业 python anaconda IDE pycharm/jupyter 熟悉基础语法,了解数据结构 刷 ...
- py 二级习题(turtle)
用turtle画一个正方形 import turtle turtle.penup() turtle.goto(-100,-100) turtle.pendown() turtle.begin_fill ...
- mybatis第二天01
MyBatis第二天01 1.高级结果映射 1.1根据视频案例,分析表之间的关系 数据模型分析 1. 明确每张表存储的信息 2. 明确每张表中关键字段(主键.外键.非空) 3. 明确数据库中表与表之间 ...
- Wannafly Camp 2020 Day 2K 破忒头的匿名信 - AC自动机,dp
给定字典和文章,每个单词有价值,求写文章的最小价值 标准的 AC 自动机 dp,设 \(f[i]\) 表示写 \(s[1..i]\) 的最小价值,建立AC自动机后根据 trans 边暴力转移即可 建了 ...