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 树上贪心的更多相关文章

  1. 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号结 ...

  2. 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 ...

  3. 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 ...

  4. Codeforces Round #530 (Div. 1)

    A - Sum in the tree 就是贪心选尽量让上面的点权尽量大,那么对于偶数层的点,其到根节点的和即为所有儿子中的最大值. #include<bits/stdc++.h> usi ...

  5. 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 ...

  6. Codeforces Round #530 (Div. 2) A,B,C,D

    A. Snowball 链接:http://codeforces.com/contest/1099/problem/A 思路:模拟 代码: #include<bits/stdc++.h> ...

  7. Codeforces Round #530 (Div. 2)

    RANK :2252 题数 :3 补题: D - Sum in the tree 思路:贪心 把权值放在祖先节点上 ,预处理 每个节点保存 他与他儿子中 权值最小值即可. 最后会有一些叶子节点依旧为 ...

  8. Codeforces Round #530 (Div. 2) Solution

    A. Snowball 签. #include <bits/stdc++.h> using namespace std; ], d[]; int main() { while (scanf ...

  9. Codeforces Round #530 (Div. 2) F (树形dp+线段树)

    F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...

随机推荐

  1. the simmon effect(in psychology) :build the function of subject_information(modify the experiment programme),before we begin the experiment

    #the real experiment for simon effect #load the library which is our need import pygame import sys i ...

  2. Selenium实战(二)——调用JavaScript之execute_script()方法

    1.浏览器滚动条的拖动,不能依靠WebDriver提供的API来实现,用于调整浏览器滚动条位置的JavaScript代码如下: window.scrollTo(0,450); window.scrol ...

  3. C语言库函数strstr、strch比较

    该库函数包含在<string.h>头文件中,函数原型:extern char *strstr(char *str1, const char *str2);使用方法 char *strstr ...

  4. Vue中axios有关请求头的几点小结

    在Vue前端中向后端发起http请求会有着两种写法:一种是在vue文件中直接导入axios模板,另外一种是使用Vue的属性$http. 1.在第一种方式中,在同一个工程中所添加的vue文件直接使用ax ...

  5. 小程序图片上传,长按删除,weui

    <view class="weui-cells"> <view class="weui-cell"> <view class=&q ...

  6. lampp ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'lepus'

    解决方法: 在[mysqlld]段下增加如下代码:skip-grant-tables: 1.which mysql 查看mysql位置,例如:/opt/lampp/bin/mysql 2.进入配置my ...

  7. PHP中关于foreach使用引用变量的坑

    PHP版本为 5.6.12 代码如下:   1 2 3 4 5 6 7 8 9 10 11 12 <?php $arr = ['a', 'b', 'c', 'd', 'e']; foreach ...

  8. arm汇编笔记

    ARM汇编(非虫笔记) 1.ARM汇编的目的: 分析elf文件的需要. 2.原生程序生成过程. (1)预处理,编译器处理c代码中的预处理指令. gcc -E hello.c -o hello.i (2 ...

  9. vue使用饿了么element-ui框架中的上传组件进度条无法使用,:on-progress钩子无法触发的原因

    自己写的例子都是好好的,调试了半天,在项目里怎么都出不来 最终终于找到原因: 在上传文件时要做进度显示需要用到xhr.upload.onprogress事件,此时如果你的项目里用到mock.js模拟数 ...

  10. IntelliJ WebStorm 最新版 安装永久破解教程【最强,可用至2099年】

    IntelliJ WebStorm 2018.3.6安装永久破解[最强]  一. 在官网下载WebStorm安装包  链接:http://www.jetbrains.com/webstorm/down ...