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块饼干,在这个节点上的每块饼干 ...
随机推荐
- python常见函数积累
shape() 返回数组或者数据框有多少行或者多少列 import numpy as np x = np.array([[1,2,5],[2,3,5],[3,4,5],[2,3,6]]) #输出数组的 ...
- sql server针对字符串型数字排序(针对此字符串的长度不一致)
对于不规则的字符串数字排序,无法按照数字大的大小排序的原因是,字符串数字在数据库中按照ASCII码排序,从字符的第一个数字对比,首先就会将为首个数字相同的排在一起,在从这些字符串里面对比第二个数字,如 ...
- python 轮询,长轮询
轮询相关 用于消息和投票等 轮询 1.采用js 定时请求. html <!DOCTYPE html> <html lang="zh-CN"> <hea ...
- 一直报找不到function,然后又不为null。最后发现是个数组。哭死。
今天写代码,报找不到function.然后又不为空.最后发现是类型不对.因为是数组,所以不能用node.function(),而应该用nodeArr[0].function.错用前者会找不到funct ...
- ctf-ping命令执行绕过
题目连接:http://ctf.klmyssn.com/challenges#Ping 命令执行绕过,试了试过滤了一些:一些命令 但是反引号可以执行命令 通过拼接,可以拼接出来:ls 命令 127.0 ...
- 题解【洛谷P1083】[NOIP2012]借教室
题面 二分到哪一个申请人要修改订单,可以差分\(+\)前缀和达到\(\Theta(n)\)的\(\text{check}\). 具体细节见代码. #include <bits/stdc++.h& ...
- Application Characteristics Of LED Keychain
LED keychains are essential camping and travel accessories. Very useful as a light source. There are ...
- 【安卓开发】Webview简单使用
什么是WebView? 答:Android内置webkit内核的高性能浏览器,而WebView则是在这个基础上进行封装后的一个 控件,WebView直译网页视图,我们可以简单的看作一个可以嵌套到界面上 ...
- centos 部署 aspnetMVC 网页
在Linux上运行ASP.NET网站或WebApi的传统步骤是,先安装libgdiplus,再安装mono,然后安装Jexus.在这个过程中,虽然安装Jexus是挺简便的一件事,但是安装mono就相对 ...
- BK: Data mining, Chapter 2 - getting to know your data
Why: real-world data are typically noisy, enormous in volume, and may originate from a hodgepodge of ...