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块饼干,在这个节点上的每块饼干 ...
随机推荐
- how to use bookdown
模板下载 {#moban} 谢益辉 通用模板:https://github.com/rstudio/bookdown-demo 李东风中文模板: http://www.math.pku.edu.cn/ ...
- P1268 树的重量【构造】
题目描述 树可以用来表示物种之间的进化关系.一棵“进化树”是一个带边权的树,其叶节点表示一个物种,两个叶节点之间的距离表示两个物种的差异.现在,一个重要的问题是,根据物种之间的距离,重构相应的“进化树 ...
- 关于Windows系统下端口被占用的问题和task命令
一.如何解决端口被占用的问题? 此时端口4444被进程占用,只要找到端口4444的进程,并且将进程kill掉即可. 开始--运行--cmd 进入命令提示符 输入netstat -aon 即可看到所有连 ...
- Linux shell unzip和tar 批量解压文件
#!/bin/sh UNTAR="tar -xvf " #unzip all zip files function unzip_all_file() { for i in *.zi ...
- H5_0020:判断安卓苹果平台
var u = navigator.userAgent, app = navigator.appVersion; var isAndroid = u.indexOf('Android') > - ...
- VSCode常用插件之Simple New File使用
更多VSCode插件使用请访问:VSCode常用插件汇总 Simple New File这是一个从命令面板简单创建新文件.文件夹.通过侧边栏创建新文件可能会很繁琐.该扩展旨在通过命令面板使创建文件或目 ...
- 曼孚科技:AI机器学习领域常用的15个术语
机器学习是人工智能(AI)的核心,是使计算机具有智能的根本途径. 本文整理了一下机器学习领域常用的15个术语,希望可以帮助大家更好的理解这门涉及概率论.统计学.逼近论.凸分析.算法复杂度理论等多个领 ...
- [Python]pyhon去除txt文件重复行 python 2020.2.10
代码如下: import shutil readPath='E:/word4.txt' #要处理的文件 writePath='E:/word5.txt' #要写入的文件 lines_seen=set( ...
- MPI Maelstrom POJ - 1502 floyd
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> usin ...
- 《深入理解java虚拟机》读书笔记九——第十章
第十章 早期(编译期)优化 1.Javac的源码与调试 编译期的分类: 前端编译期:把*.java文件转换为*.class文件的过程.例如sun的javac.eclipseJDT中的增量编译器. JI ...