RANK :2252 题数 :3

补题:

D - Sum in the tree

思路:贪心 把权值放在祖先节点上 ,预处理 每个节点保存 他与他儿子中 权值最小值即可。

最后会有一些叶子节点依旧为 INF 权值按0算即可,然后其他的权值计算为 它 - 它父亲的。

注意判断时候会出现父亲比儿子大的这种非法情况。

#include<bits/stdc++.h>
using namespace std;
#define inf 0x7f7f7f7f
#define ll long long
#define maxn 123456
ll fa[maxn],n,s[maxn],ans;
int main()
{
scanf("%lld",&n);
for(int i=2; i<=n; i++)
scanf("%lld",&fa[i]);
for(int i=1; i<=n; i++)
{
scanf("%lld",&s[i]);
if(s[i]==-1)s[i]=inf;
}
ans=s[1];
for(int i=2; i<=n; i++)
s[fa[i]]=min(s[fa[i]],s[i]);
for(int i=2; i<=n; i++)
{
if(s[i]==inf)s[i]=0;
else
{
ans+=s[i]-s[fa[i]];
if(s[i]-s[fa[i]]<0)
{
printf("-1\n");
return 0;
}
}
}
printf("%lld\n",ans);
return 0;
}

  

Codeforces Round #530 (Div. 2)的更多相关文章

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

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

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

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

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

  4. Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)

    https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...

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

    题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...

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

    C: *可以保留删除或者增加 ? 保留或者删除 #include<bits/stdc++.h> using namespace std; int main(){ string s; int ...

  7. Codeforces Round #530 Div. 1 自闭记

    A:显然应该让未确定的大小尽量大.不知道写了啥就wa了一发. #include<iostream> #include<cstdio> #include<cmath> ...

  8. Codeforces Round #530 (Div. 2) F - Cookies

    F - Cookies 思路:我们先考虑如何算出在每个节点结束最多能吃多少饼干, 这个dfs的时候用线段树维护一下就好了, 然后有个这个信息之后树上小dp一下就好啦. #include<bits ...

  9. Codeforces Round #530 (Div. 1)

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

随机推荐

  1. jquery_ajax 跨域

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  2. jq插件模板

    !(function($,window, document, undefined){ function Plugin(ele,options){ //默认参数设置 this.defaults={ pa ...

  3. flask中的wtforms使用

    一.简单介绍flask中的wtforms WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证. 安装: pip3 install wtforms 二.简单使用wtfo ...

  4. 按照勾选 删除表格的行<tr>

    需求描述:有一个产品列表,有一个删减按钮,点击删减按钮,按照产品勾选的行,删除产品列表中对应的行数据 代码: //html代码<table id="table1"> & ...

  5. Java 产生一个大于等于200,小于300的随机数,且是10的整数倍

    public class Random200_300 { public static void main(String[] args) { int r1 = 0; while (true) { r1 ...

  6. time与datetime模块

    在python中,通常用下面几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量. 格式化的时间字符串(format s ...

  7. expect 安装 salt 客户端

    #!/bin/bash for i in $(cat ./host.txt) do echo $i > ./tmp.txt HOSTNAME=$(cut -d ':' -f1 ./tmp.txt ...

  8. JQuery调用WCF服务

    一:创建一个wcf服务项目 [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(RequestF ...

  9. Redis托管Session

    一:redis托管session主要是为了不同域之间共享session.Asp.net提供了四种处理Session的方法 1.  InProc模式 这是ASP.NET默认的Session管理模式,在应 ...

  10. php处理IOS图片旋转

    $picAddr = $url; $exif = exif_read_data($picAddr); $image = imagecreatefromjpeg($picAddr); if($exif[ ...